AllInfoHub Logo

AllInfoHub – MCQ Practice

Classes and Objects – Multiple Choice Questions (MCQs)

  1. 25. What are access modifiers in Java?

    • A. Keywords that control the visibility of class members
    • B. Keywords used to define data types
    • C. Keywords used for looping
    • D. Keywords used for exception handling
  2. 26. Which access modifier provides the widest level of accessibility?

    • A. private
    • B. protected
    • C. public
    • D. default (package-private)
  3. 27. Which access modifier restricts access to within the same class?

    • A. private
    • B. protected
    • C. public
    • D. default (package-private)
  4. 28. Which access modifier allows access within the same class, subclasses, and the same package?

    • A. private
    • B. protected
    • C. public
    • D. default (package-private)
  5. 29. What is the default access modifier in Java if no modifier is explicitly specified?

    • A. private
    • B. protected
    • C. public
    • D. default (package-private)
  6. 30. What is the role of the garbage collector in the context of objects?

    • A. To manually create objects
    • B. To automatically reclaim memory occupied by objects that are no longer referenced
    • C. To optimize object performance
    • D. To handle object serialization
  7. 31. What is the output of the following code? `class MyClass { int x = 10; public static void main(String[] args) { MyClass obj = new MyClass(); System.out.println(obj.x); } }`

    • A. 0
    • B. 10
    • C. Error
    • D. null
  8. 32. What is the output of the following code? `class Counter { static int count = 0; Counter() { count++; } public static void main(String[] args) { Counter c1 = new Counter(); Counter c2 = new Counter(); System.out.println(Counter.count); } }`

    • A. 0
    • B. 1
    • C. 2
    • D. Error
  9. 33. What is the output of the following code? `class Parent { void display() { System.out.println(\Parent\"); } } class Child extends Parent { void display() { System.out.println(\""Child\""); } public static void main(String[] args) { Child obj = new Child(); obj.display(); } }`"""

    • A. Parent
    • B. Child
    • C. Parent\nChild
    • D. Error
  10. 34. What is the output of the following code? `class Calculator { int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } public static void main(String[] args) { Calculator calc = new Calculator(); System.out.println(calc.add(2, 3)); System.out.println(calc.add(2, 3, 4)); } }`

    • A. 5\n9
    • B. 9\n5
    • C. Error
    • D. None
  11. 35. What is the output of the following code? `class Example { final int value = 5; void change() { // value = 10; // This line would cause an error } public static void main(String[] args) { Example obj = new Example(); System.out.println(obj.value); } }`

    • A. 5
    • B. 10
    • C. Error
    • D. None
  12. 36. What is the output of the following code? `class Test { static int count = 0; Test() { count++; } public static void main(String[] args) { Test t1 = new Test(); Test t2 = new Test(); System.out.println(t1.count); System.out.println(t2.count); System.out.println(Test.count); } }`

    • A. 1\n2\n2
    • B. 1\n1\n1
    • C. 2\n2\n2
    • D. Error