Java Coding Interview Questions – Part 4

java coding interviewImage source : today.com
 
You may also like :

Java Coding Interview Questions – Part 1

Java Coding Interview Questions – Part 2

Java Coding Interview Questions – Part 3

 

1. What will be the output of following Java program ?

package quiz;

public class Quiz22 {
  
  static String str;
  
  public static boolean test1(){
    return new Boolean("1");
  }
  
  public static boolean test2(){
    return new Boolean(str);
  }

  public static void main(String[] args) {

    if(test1())
      System.out.print("1");
    if(!test2())
      System.out.print("2");
    if(test1() != test2())
      System.out.print("3");
  }

}


   

Output

2

The Boolean(String) constructor views null Strings and Strings that don’t have value of “true” as “false”.

Unboxing automatically converts Boolean objects to boolean primitives.

So, both test1() and test2() return false. Therefore, !test2() evaluates to true and 2 is printed.

3 is not printed because test1() and test2() return boolean primitives, so they are unboxed before the function returns.

  

2. Predict the output of following Java program.

package quiz;

public class Hi extends Greeting{

  public static void main(String[] args) {

    new Hi();
    new Hi("Hi");
  }
  
  Hi(){this("Hello");}

  Hi(String s){super(s);}
}


 
package quiz;

public class Greeting {

  Greeting(String s){ System.out.print(s);}
}


    

Output

HelloHi


The no-arg constructor invokes the other constructor via “this”. The second constructor chains to the super class constructor using “super”.

 

3. What will be the output of following Java program ?

package quiz;

public class Quiz24 {
  
  private static int increment(int i){
    int num= (++i) + (i++);
    return num;
  }

  public static void main(String[] args) {
    
    for(int i=0; i < 5; increment(i)){
      System.out.print(i);
    }
  }

}


  

Output

its an infinite loop

 

In a for loop, we can use method call as the iteration expression. But, here the code fails to capture the result of the method call.

It should be i = increment(i)

Therefore, value of i does not change and its an infinite loop.

 

4. What happens when you compile and run the following program ?

package quiz;

public class Quiz25 {
  
  private static int increment(int i){
    int num= (++i) + (i++);
    return num;
  }

  public static void main(String[] args) {
    
    for(int i=0; i < 5; i = increment(i)){
      System.out.print(i);
    }
  }

}


  

Output

02


The increment portion in for loop can be a method call as well.. hence the syntax is correct.

Although, the increment is at the top of for loop, it gets executed after the for loop body (in this case, the print statement) is executed.

So, i gets printed before the increment method is called.

 

5. Predict the output of following Java program.

package quiz;

public class Quiz23 {

  public static void main(String[] args) {
    int x =0;
    
    int[] nums= {1,2,3,5};
    
    for (int i:nums)
      switch(i){
        case 1:
          x += i;
        case 5:
          x += i;
        default:
          x += i;
        case 2:
          x += i;
      }
    System.out.println(x);
  }

}


  

Output

 

27

When a case is matched in switch, its code and all the subsequent case code will run unless break is encountered.

Switch statement can be added inside for each loop and the case statement can be present in any order.

 
You may also like :

Java Coding Interview Questions – Collections

Java Coding Interview Questions – Collections (Part 2)

Java Interview Questions for Experienced Professionals

 

© 2016 – 2018, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags