Java Coding Interview Questions – Part 5

Java Coding Interview Questions-Part 5
 
You may also like :

Java Coding Interview Questions – Part 1

Java Coding Interview Questions – Part 2

Java Coding Interview Questions – Part 3

Java Coding Interview Questions – Part 4

 

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

package com.topjavatutorial.quiz;

public class StringConcatenation {

  public static void main(String[] args) {
    int one = 1;
    String two = "2";
    System.out.print(two + 1 + one);
    System.out.println(one + 1 + two);
  }

}

   

Output

21122

  

Explanation

Here are some rules for this :

1) When one of the operands in + is string, its concatenated. The result is a String.

2) When both operands are numbers, numbers are added.

3) The operation is evaluated from left to right.

We have,

int one =1;
String two = “2”;

Now,

two + 1 + one
= (“2” + 1) + 1 //left to right evaluation
= “21” + 1
= “211”

Similarly,

one + 1 + two
= (1 + 1) + “2” //left to right evaluation
=”22″

So, answer is 21122

Read more on this here:

Java + operator and Operator Overloading

  

2. Predict the output of following Java program.

package com.topjavatutorial.quiz;

public class IntegerComparison {

  public static void main(String[] args) {
    Integer i1 = -128;
    Integer i2 = -128;
    Integer i3 = 128;
    Integer i4 = 128;
    System.out.println((i1==i2) + "," + (i3==i4));
  }

}

    

Output

true,false

 

Explanation

When we assign a literal number to Integer, compiler adds Integer.valueOf for the literal.

For example,

Integer i1= -128 becomes Integer i1= Integer.valueOf(-128)

In valueOf implementation, by default integers -128 to 127 are cached and the cached instance is returned for the integer.

Therefore i1==i2 returns true as they are referring to same cached instance.

However, 128 is not cached, so i3==i4 returns false.

The default high value until which integers are cached is 127. So, values from -128 till 127 is cached.

The high value up to which Integers are cached can be updated using the system property java.lang.Integer.IntegerCache.high


 

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

package com.topjavatutorial.quiz;

public interface InterfaceA {
  default String getMessage() {
    return "Hola";
  }
}

package com.topjavatutorial.quiz;

public class ClassA {
  public String getMessage() {
    return "Hello";
  }
}

package com.topjavatutorial.quiz;

public class DemoClass extends ClassA implements InterfaceA {

  public static void main(String[] args) {
    System.out.println(new DemoClass().getMessage());
  }

}

  

Output

Hello

 

Explanation


If a class inherits a method (abstract or concrete) from its
superclass and a method with the same signature from one of its super-interfaces, then the class inherits the method of the superclass and the methods in the super-interfaces are ignored.

This rule treats a default method in an interface as a fallback if the same method is not available in the class through the class hierarchy.

 

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

package com.topjavatutorial.quiz;

import java.util.Scanner;

public class ReadingInputUsingScanner {

  public static void main(String[] args) {
    Scanner sc = new Scanner("true false 0");
    while(sc.hasNext()){
      if(sc.hasNextBoolean())
        System.out.print(sc.nextBoolean() + " ");
    }
    sc.close();
  }

}

  

Output

true false followed by infinite loop

 

Explanation

The while loop iterates over some data and prints if it finds a Boolean value.

So, it prints true and false. But when it finds 0, the program never moves to next token since it is not Boolean.

The correct syntax to get only Boolean values would be :

      if(sc.hasNextBoolean())
        System.out.println(sc.nextBoolean());
      else
        sc.next();

 

5. Predict the output of following Java program.

package com.topjavatutorial.quiz;

import java.text.NumberFormat;
import java.text.ParseException;

public class NumberFormatQuiz {

  public static void main(String[] args) {
    NumberFormat fmt = NumberFormat.getInstance();
    try {
      Number num = fmt.parse("123xyz456");
      System.out.println(num);
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }

}

  

Output

 

123

 

Explanation

parse() method in NumberFormat parses text from the beginning of the given string to produce a number. The method may not use the entire text of the given string.

It will throw an exception if beginning of a string can’t be parsed.

For example, it will throw ParseException for string “A123xyz456”

 
You may also like :

Java Coding Interview Questions – Collections

Java Coding Interview Questions – Collections (Part 2)

Java Coding Interview Questions – Part 3

Java Coding Interview Questions – Part 4

 

© 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