Object-Oriented Programming
Difficulty: medium
Overview
OOP pillars: Encapsulation (private fields + public getters/setters), Abstraction, Inheritance, Polymorphism. Constructors share the class name and have no return type. "this" refers to the current instance. static members belong to the class. Method overloading = same name, different parameters (compile-time polymorphism). The garbage collector automatically reclaims unreachable objects.
Practice Linked Questions
Q1. Which keyword is used to create a new object in Java?
Select one answer before revealing.
Q2. In Java, a constructor must:
Select one answer before revealing.
Q3. Which access modifier makes a member accessible only within the declaring class?
Select one answer before revealing.
Q4. Defining multiple methods with the same name but different parameter lists in the same class is called:
Select one answer before revealing.
Q5. Which statement about static methods in Java is true?
Select one answer before revealing.
Q6. What is a default constructor in Java?
Select one answer before revealing.
Q7. Which of the following are valid access modifiers in Java? (More than one answer may be correct)
Select one answer before revealing.
Q8. Which statement about the 'this' keyword in Java is true?
Select one answer before revealing.
Q9. Encapsulation in Java is best achieved by:
Select one answer before revealing.
Q10. Which of the following are true about constructors in Java? (More than one answer may be correct)
Select one answer before revealing.
Q11. What is the role of Java's garbage collector?
Select one answer before revealing.
Q12. What is the purpose of the finalize() method in Java?
Select one answer before revealing.
Q13. What is a getter method in Java?
Select one answer before revealing.
Q14. What is the output of the following code? class Counter { static int count = 0; Counter() { count++; } } Counter a = new Counter(); Counter b = new Counter(); Counter c = new Counter(); System.out.println(Counter.count);
Select one answer before revealing.
Q15. Which of the following code blocks compile and run without error? (More than one answer may be correct) A: final int x; x = 5; System.out.println(x); B: final int y = 5; y = 10; System.out.println(y); C: final int[] arr = {1,2,3}; arr[0] = 99; System.out.println(arr[0]); D: final String s = "hello"; s = "world"; System.out.println(s);
Select one answer before revealing.
Q16. What is the output of the following code? class Test { static { System.out.println("Static"); } { System.out.println("Instance"); } Test() { System.out.println("Constructor"); } public static void main(String[] args) { System.out.println("Main"); new Test(); new Test(); } }
Select one answer before revealing.
Q17. What is the output of the following code? enum Day { MON, TUE, WED, THU, FRI, SAT, SUN } Day d = Day.WED; System.out.println(d.ordinal()); System.out.println(d.name());
Select one answer before revealing.
Q18. What is the output of the following code? static <T extends Number> double sum(List<T> list) { return list.stream().mapToDouble(Number::doubleValue).sum(); } List<Integer> ints = Arrays.asList(1, 2, 3); System.out.println(sum(ints));
Select one answer before revealing.
Q19. What is the output of the following code? class Outer { private int x = 10; class Inner { void display() { System.out.println(x); } } } Outer o = new Outer(); Outer.Inner i = o.new Inner(); i.display();
Select one answer before revealing.
Q20. What is the output of the following code? static int sum(int... nums) { int total = 0; for (int n : nums) total += n; return total; } System.out.println(sum(1, 2, 3)); System.out.println(sum()); System.out.println(sum(new int[]{5, 10}));
Select one answer before revealing.
Q21. What is true about the following code due to type erasure? List<String> strings = new ArrayList<>(); List<Integer> ints = new ArrayList<>(); System.out.println(strings.getClass() == ints.getClass());
Select one answer before revealing.
Q22. Which of the following are valid usages from within a static method of class Foo? (More than one answer may be correct) // class Foo { static int x = 5; int y = 10; } A: System.out.println(x); B: System.out.println(y); C: System.out.println(new Foo().y); D: System.out.println(this.x);
Select one answer before revealing.
Q23. Which of the following statements about Java generics are true? (More than one answer may be correct) A: List<Integer> is a subtype of List<Object> B: List<?> accepts both List<Integer> and List<String> as arguments C: You can write: T obj = new T(); inside a generic method D: String[] is a subtype of Object[] (arrays are covariant)
Select one answer before revealing.
Q24. What is the output of the following code? int[] a = {1, 2, 3}; int[] b = a; b[0] = 99; System.out.println(a[0]); System.out.println(a == b);
Select one answer before revealing.