Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Java Interview Coding problems for Freshers- Part III

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 272
    Comment on it

    In the previous part, Java Interview Questions and Answers Part I and Java Interview Coding problems for Freshers- Part II, we have explained some basic questions of Java. This part is in continuation with the previous Java Interview Questions and Answers tutorial. In this part of the tutorial we will proceed with more questions based on further concepts of Java.

    21 - Write a program to concatenate string using for Loop

    /*Example:

    Input 5

    Output - 1 2 3 4 5 */

    class Join{ 
    public static void main(String args[]){ 
    int num = Integer.parseInt(args[0]); 
    String result = " "; 
    for(int i=1;i<=num;i++){ 
    result = result + i + " "; 
    } 
    System.out.println(result); 
    } 
    } 
    

    Output of the program would be when argument passes is 10

    /*
    1 2 3 4 5 6 7 8 9 10
    */
    

    22 - Program to Display Multiplication Table

    class MultiplicationTable{ 
    public static void main(String args[]){ 
    int num = Integer.parseInt(args[0]); 
    System.out.println("*****MULTIPLICATION TABLE*****"); 
    for(int i=1;i<=num;i++){ 
    for(int j=1;j<=num;j++){ 
    System.out.print(" "+i*j+" "); 
    } 
    System.out.print("\n"); 
    } 
    } 
    } 
    Output of the program would be when argument passed is 5
    /*
    *****MULTIPLICATION TABLE*****
    1  2  3  4  5
    2  4  6  8  10
    3  6  9  12  15
    4  8  12  16  20
    */
    

    Program 23 - Write a program to Swap the values

    class Swap{ 
    public static void main(String args[]){ 
    int num1 = Integer.parseInt(args[0]); 
    int num2 = Integer.parseInt(args[1]); 
    System.out.println("\n***Before Swapping***"); 
    System.out.println("Number 1 : "+num1); 
    System.out.println("Number 2 : "+num2); 
    //Swap logic 
    num1 = num1 + num2; 
    num2 = num1 - num2; 
    num1 = num1 - num2; 
    System.out.println("\n***After Swapping***"); 
    System.out.println("Number 1 : "+num1); 
    System.out.println("Number 2 : "+num2); 
    } 
    } 
    

    Output of the program would be when num1=10 and num2=5

    /*
    ***Before Swapping***
    Number 1 : 10
    Number 2 : 5
    
    ***After Swapping***
    Number 1 : 5
    Number 2 : 10
    
    */
    

    24 - Write a program to convert given no. of days into months and days.(Assume that each month is of 30 days)

    /*Example :

    Input 69

    Output - 69 days = 2 Month and 9 days */

    class DayMonthDemo{ 
    public static void main(String args[]){ 
    int num = Integer.parseInt(args[0]); 
    int days = num%30; 
    int month = num/30; 
    System.out.println(num+" days = "+month+" Month and "+days+" days"); 
    } 
    } 
    

    Output of the program would be when argument passed is 66

    /*
    66 days = 2 Month and 6 days
    */
    

    25 - Write a program to find whether given no. is Armstrong or not.

    /*Example :

    Input 153

    Output - 1^3 + 5^3 + 3^3 = 153, so it is Armstrong no. */

    class Armstrong{ 
    public static void main(String args[]){ 
    int num = Integer.parseInt(args[0]); 
    int n = num; //use to check at last time 
    int check=0,remainder; 
    while(num > 0){ 
    remainder = num % 10; 
    check = check + (int)Math.pow(remainder,3); 
    num = num / 10; 
    } 
    if(check == n) 
    System.out.println(n+" is an Armstrong Number"); 
    else 
    System.out.println(n+" is not a Armstrong Number"); 
    } 
    } 
    

    Output of the Armstrong program would be

    /*
    sh-4.3$ javac Armstrong.java
    sh-4.3$ java-Xmx128M-Xms16M  Armstrong
    10 is not a Armstrong Number
    sh-4.3$ javac Armstrong .java
    sh-4.3$ java -Xmx128M -Xms16M  Armstrong
    153 is an Armstrong Number
    */
    

    26 - Switch case demo

    /*Example : Input - 124 Output - One Two Four */

    class SwitchCaseDemo{ 
    public static void main(String args[]){ 
    try{ 
    int num = Integer.parseInt(args[0]); 
    int n = num; //used at last time check 
    int reverse=0,remainder; 
    while(num > 0){ 
    remainder = num % 10; 
    reverse = reverse * 10 + remainder; 
    num = num / 10; 
    } 
    String result=""; //contains the actual output 
    while(reverse > 0){ 
    remainder = reverse % 10; 
    reverse = reverse / 10; 
    switch(remainder){ 
    case 0 : 
    result = result + "Zero "; 
    break; 
    case 1 : 
    result = result + "One "; 
    break; 
    case 2 : 
    result = result + "Two "; 
    break; 
    case 3 : 
    result = result + "Three "; 
    break; 
    case 4 : 
    result = result + "Four "; 
    break; 
    case 5 : 
    result = result + "Five "; 
    break; 
    case 6 : 
    result = result + "Six "; 
    break; 
    case 7 : 
    result = result + "Seven "; 
    break; 
    case 8 : 
    result = result + "Eight "; 
    break; 
    case 9 : 
    result = result + "Nine "; 
    break; 
    default: 
    result=""; 
    } 
    } 
    System.out.println(result); 
    }catch(Exception e){ 
    System.out.println("Invalid Number Format"); 
    } 
    } 
    } 
    

    Output of the Switch case program would be when argument passed is 7896

    /*
    Seven Eight Nine Six
    */
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: