Java interview questions part 7

Java interview questions part 7

Take as many assements as you can to improve your validate your skill rating

Total Questions: 20

1. Why are generics used?

Correct Answer is : Generics add stability to your code by making more of your bugs detectable at compile time

2. Which of these type parameters is used for a generic class to return and accept any type of object?

Correct Answer is : T

3. Which of these type parameters is used for a generic class to return and accept a number?

Correct Answer is : N

4. Which of these is an correct way of defining generic class?

Correct Answer is : class name { /* … */ }

5. Which of the following is an incorrect statement regarding the use of generics and parameterized types in Java?

Correct Answer is : When designing your own collections class (say, a linked list), generics and parameterized types allow you to achieve type safety with just a single class definition as opposed to defining multiple classes

6. Which of the following reference types cannot be generic?

Correct Answer is : Anonymous inner class

7. What is the output of this program? public class BoxDemo { public static void addBox(U u, java.util.List> boxes) { Box box = new Box(); box.set(u); boxes.add(box); } public static void outputBoxes(java.util.List> boxes) { int counter = 0; for (Box box: boxes) { U boxContents = box.get(); System.out.println("Box #" + counter + " contains [" + boxContents.toString() + "]"); counter++; } } public static void main(String[] args) { java.util.ArrayList> listOfIntegerBoxes = new java.util.ArrayList(); BoxDemo.addBox(Integer.valueOf(10), listOfIntegerBoxes); BoxDemo.outputBoxes(listOfIntegerBoxes); } }

Correct Answer is : Box #0 contains [10].

8. What is the output of this program? public class BoxDemo { public static void addBox(U u, java.util.List> boxes) { Box box = new Box(); box.set(u); boxes.add(box); } public static void outputBoxes(java.util.List> boxes) { int counter = 0; for (Box box: boxes) { U boxContents = box.get(); System.out.println("[" + boxContents.toString() + "]"); counter++; } } public static void main(String[] args) { java.util.ArrayList> listOfIntegerBoxes = new java.util.ArrayList(); BoxDemo.addBox(Integer.valueOf(0), listOfIntegerBoxes); BoxDemo.outputBoxes(listOfIntegerBoxes); } }

Correct Answer is : [0].

9. What is the output of this program? import java.util.*; public class genericstack { Stack stk = new Stack (); public void push(E obj) { stk.push(obj); } public E pop() { E obj = stk.pop(); return obj; } } class Output { public static void main(String args[]) { genericstack gs = new genericstack(); gs.push("Hello"); System.out.print(gs.pop() + " "); genericstack gs = new genericstack(); gs.push(36); System.out.println(gs.pop()); } }

Correct Answer is : Hello 36

10. What is the output of this program? public class BoxDemo { public static void addBox(U u, java.util.List> boxes) { Box box = new Box(); box.set(u); boxes.add(box); } public static void outputBoxes(java.util.List> boxes) { int counter = 0; for (Box box: boxes) { U boxContents = box.get(); System.out.println("Box #" + counter + " contains [" + boxContents.toString() + "]"); counter++; } } public static void main(String[] args) { java.util.ArrayList> listOfIntegerBoxes = new java.util.ArrayList(); BoxDemo.addBox(Integer.valueOf(10), listOfIntegerBoxes); BoxDemo.outputBoxes(listOfIntegerBoxes); } }

Correct Answer is : Box #0 contains [10].

11. Which of the following access specifiers can be used for an interface?

Correct Answer is : Protected

12. Which of the following is the correct way of implementing an interface A by class B?

Correct Answer is : class B implements A{}

13. All methods must be implemented of an interface.

Correct Answer is : TRUE

14. What type of variable can be defined in an interface?

Correct Answer is : static final

15. What does an interface contain?

Correct Answer is : Method declaration

16. What type of methods an interface contain by default?

Correct Answer is : abstract

17. What will happen if we provide concrete implementation of method in interface?

Correct Answer is : Compilation failure

18. What happens when a constructor is defined for an interface?

Correct Answer is : Compilation failure

19. What happens when we access the same variable defined in two interfaces implemented by the same class?

Correct Answer is : The interfaceName.variableName needs to be defined

20. Can “abstract” keyword be used with constructor, Initialization Block, Instance Initialization and Static Initialization Block.

Correct Answer is : FALSE

Similar Interview Questions

    Search for latest jobs

    Icon
    Icon