Java OOP Essentials: 136 MCQs
All 136 multiple-choice questions from this kit, free and with no sign-up.
Warm-up · 63 questions
Q1. Which access modifier allows access only within the same class?
BeginnerAnswers and explanations are in the Veda app.
Q2. What is the default access level in Java when no modifier is written?
BeginnerAnswers and explanations are in the Veda app.
Q3. Which access modifier allows access within the same package and to subclasses in other packages?
BeginnerAnswers and explanations are in the Veda app.
Q4. What is the main benefit of data hiding?
BeginnerAnswers and explanations are in the Veda app.
Q5. Which of the following is NOT a benefit of encapsulation?
BeginnerAnswers and explanations are in the Veda app.
Q6. A `protected` member is accessible to any class in the same package and to subclasses in other packages.
BeginnerAnswers and explanations are in the Veda app.
Q8. List the four access levels in Java from most restrictive to least restrictive.
BeginnerAnswers and explanations are in the Veda app.
Q9. What is the visibility of a `private` member?
BeginnerAnswers and explanations are in the Veda app.
Q10. What is the default access level in Java when no modifier is specified?
BeginnerAnswers and explanations are in the Veda app.
Q11. What is the visibility of a `protected` member?
BeginnerAnswers and explanations are in the Veda app.
Q12. What is the visibility of a `public` member?
BeginnerAnswers and explanations are in the Veda app.
Q16. What is a common pattern that uses encapsulation in Java?
BeginnerAnswers and explanations are in the Veda app.
Q17. What is a class in Java?
BeginnerAnswers and explanations are in the Veda app.
Q18. Which keyword is used to create an object in Java?
BeginnerAnswers and explanations are in the Veda app.
Q19. What does a reference variable store?
BeginnerAnswers and explanations are in the Veda app.
Q20. Which of the following correctly creates a new `Dog` object?
BeginnerAnswers and explanations are in the Veda app.
Q21. Which of the following is a primitive type in Java?
BeginnerAnswers and explanations are in the Veda app.
Q22. What does the dot operator (`.`) do in Java?
BeginnerAnswers and explanations are in the Veda app.
Q23. If a class has no constructor defined, what happens?
BeginnerAnswers and explanations are in the Veda app.
Q24. A reference variable stores the actual object data.
BeginnerAnswers and explanations are in the Veda app.
Q25. Declaring a reference variable creates an object in memory.
BeginnerAnswers and explanations are in the Veda app.
Q28. How do you access fields and methods of an object?
BeginnerAnswers and explanations are in the Veda app.
Q29. What is the default value of a reference variable that has not been assigned an object?
BeginnerAnswers and explanations are in the Veda app.
Q30. What exception is thrown when you call a method on a `null` reference?
BeginnerAnswers and explanations are in the Veda app.
Q33. If no constructor is defined in a class, what happens?
BeginnerAnswers and explanations are in the Veda app.
Q34. Do two objects of the same class share their instance fields?
BeginnerAnswers and explanations are in the Veda app.
Q35. What is the result of `Car myCar;` without `new`?
BeginnerAnswers and explanations are in the Veda app.
Q36. What is the purpose of the dot operator?
BeginnerAnswers and explanations are in the Veda app.
Q37. Which of the following best describes a static field?
BeginnerAnswers and explanations are in the Veda app.
Q38. Which of the following is a valid way to call a static method `foo()` of class `MyClass`?
BeginnerAnswers and explanations are in the Veda app.
Q39. When is a static block executed?
BeginnerAnswers and explanations are in the Veda app.
Q40. Which of the following is a correct declaration of a static constant?
BeginnerAnswers and explanations are in the Veda app.
Q41. A static block executes every time a new object of the class is created.
BeginnerAnswers and explanations are in the Veda app.
Q42. What is the difference between a static field and an instance field?
BeginnerAnswers and explanations are in the Veda app.
Q43. Why is the `main` method declared static?
BeginnerAnswers and explanations are in the Veda app.
Q44. What is a static block and when does it run?
BeginnerAnswers and explanations are in the Veda app.
Q45. Give an example of a static constant and explain why it is useful.
BeginnerAnswers and explanations are in the Veda app.
Q46. Can a static method be called using an object reference? Is it recommended?
BeginnerAnswers and explanations are in the Veda app.
Q47. What happens if you try to access an instance variable from a static method without an object reference?
BeginnerAnswers and explanations are in the Veda app.
Q48. What is the purpose of a static initializer block?
BeginnerAnswers and explanations are in the Veda app.
Q49. Can a static method access another static method directly?
BeginnerAnswers and explanations are in the Veda app.
Q50. What is the primary purpose of a constructor in Java?
BeginnerAnswers and explanations are in the Veda app.
Q51. If a class defines no constructors, what does Java provide?
BeginnerAnswers and explanations are in the Veda app.
Q52. What is the effect of defining any constructor in a class?
BeginnerAnswers and explanations are in the Veda app.
Q53. Which of the following correctly describes a parameterized constructor?
BeginnerAnswers and explanations are in the Veda app.
Q54. What is constructor overloading?
BeginnerAnswers and explanations are in the Veda app.
Q55. What does the `this` keyword refer to in a constructor?
BeginnerAnswers and explanations are in the Veda app.
Q56. The default constructor is provided by the compiler only if the class has no other constructors.
BeginnerAnswers and explanations are in the Veda app.
Q57. A constructor can be called using dot notation on an existing object.
BeginnerAnswers and explanations are in the Veda app.
Q60. When does the default constructor disappear?
BeginnerAnswers and explanations are in the Veda app.
Q63. What does the `this` keyword refer to in a constructor?
BeginnerAnswers and explanations are in the Veda app.
Building up · 49 questions
Q64. What is the primary purpose of making fields private and providing public getters/setters?
IntermediateAnswers and explanations are in the Veda app.
Q65. Consider a `BankAccount` class with a private `balance` field. Which method correctly enforces a rule that prevents negative balance?
IntermediateAnswers and explanations are in the Veda app.
Q66. Why does encapsulation reduce coupling between classes?
IntermediateAnswers and explanations are in the Veda app.
Q67. Which of the following is a common mistake that breaks encapsulation?
IntermediateAnswers and explanations are in the Veda app.
Q68. What is the principle of least privilege in the context of access modifiers?
IntermediateAnswers and explanations are in the Veda app.
Q69. In the context of encapsulation, what does a setter method typically do besides updating the field?
IntermediateAnswers and explanations are in the Veda app.
Q70. Why does encapsulation improve maintainability?
IntermediateAnswers and explanations are in the Veda app.
Q71. Encapsulation is the same as data hiding.
IntermediateAnswers and explanations are in the Veda app.
Q72. How do you enforce encapsulation for a field?
IntermediateAnswers and explanations are in the Veda app.
Q73. What is the principle of least privilege in access control?
IntermediateAnswers and explanations are in the Veda app.
Q74. What is the relationship between encapsulation and data hiding?
IntermediateAnswers and explanations are in the Veda app.
Q75. What happens if you make a field public?
IntermediateAnswers and explanations are in the Veda app.
Q76. Why does encapsulation improve maintainability?
IntermediateAnswers and explanations are in the Veda app.
Q77. What happens when you assign `obj2 = obj1;` where both are reference variables of the same type?
IntermediateAnswers and explanations are in the Veda app.
Q78. What is the primary difference between a class and an object?
IntermediateAnswers and explanations are in the Veda app.
Q79. What is the state of an object?
IntermediateAnswers and explanations are in the Veda app.
Q80. What happens if you call a method on a `null` reference?
IntermediateAnswers and explanations are in the Veda app.
Q81. Which of the following correctly describes the behavior of an object?
IntermediateAnswers and explanations are in the Veda app.
Q82. What is the purpose of a constructor?
IntermediateAnswers and explanations are in the Veda app.
Q83. Which of the following statements about instance fields is true?
IntermediateAnswers and explanations are in the Veda app.
Q84. What is the difference between a primitive type and a reference type?
IntermediateAnswers and explanations are in the Veda app.
Q85. What happens when you assign one reference variable to another?
IntermediateAnswers and explanations are in the Veda app.
Q87. Why are classes considered reusable?
IntermediateAnswers and explanations are in the Veda app.
Q88. Why can't a static method directly access an instance variable?
IntermediateAnswers and explanations are in the Veda app.
Q89. What happens if a subclass declares a static method with the same signature as a static method in its parent?
IntermediateAnswers and explanations are in the Veda app.
Q90. Consider the following code: ```java class A { static void display() { System.out.println("A"); } } class B extends A { static void display() { System.out.println("B"); } } public class Test { public static void main(String[] args) { A obj = new B(); obj.display(); } } ``` What is the output?
IntermediateAnswers and explanations are in the Veda app.
Q91. Which of the following is an appropriate use of a static method?
IntermediateAnswers and explanations are in the Veda app.
Q92. What is the main advantage of using static constants?
IntermediateAnswers and explanations are in the Veda app.
Q93. Static methods can be overridden in subclasses.
IntermediateAnswers and explanations are in the Veda app.
Q94. Can a static method access an instance variable? Explain.
IntermediateAnswers and explanations are in the Veda app.
Q95. What does it mean that static methods are 'hidden' rather than overridden?
IntermediateAnswers and explanations are in the Veda app.
Q96. Why are utility methods like `Math.sqrt()` static?
IntermediateAnswers and explanations are in the Veda app.
Q97. What is a common use of static fields for shared state?
IntermediateAnswers and explanations are in the Veda app.
Q98. Explain the difference between `static` and `final`.
IntermediateAnswers and explanations are in the Veda app.
Q99. How does the JVM call the `main` method if no object exists?
IntermediateAnswers and explanations are in the Veda app.
Q100. Which of the following is a valid use of `this` in a constructor?
IntermediateAnswers and explanations are in the Veda app.
Q101. What is the rule about `this(...)` in a constructor?
IntermediateAnswers and explanations are in the Veda app.
Q102. What is the correct initialization order when creating an object?
IntermediateAnswers and explanations are in the Veda app.
Q103. What happens if you try to call `new Student()` after defining only `Student(String name)`?
IntermediateAnswers and explanations are in the Veda app.
Q104. Why might a constructor throw an exception like `IllegalArgumentException`?
IntermediateAnswers and explanations are in the Veda app.
Q105. Consider the following code: ```java class Student { String name; int age; Student() { this("Unknown", 0); } Student(String name, int age) { this.name = name; this.age = age; } } ``` What is the purpose of the no-argument constructor?
IntermediateAnswers and explanations are in the Veda app.
Q106. What is the main benefit of using `this(...)` to delegate constructors?
IntermediateAnswers and explanations are in the Veda app.
Q107. What is the rule about `this(...)` in a constructor?
IntermediateAnswers and explanations are in the Veda app.
Q108. What is the initialization order when creating an object?
IntermediateAnswers and explanations are in the Veda app.
Q109. Why do constructors have no return type?
IntermediateAnswers and explanations are in the Veda app.
Q110. How can a constructor enforce object invariants?
IntermediateAnswers and explanations are in the Veda app.
Q111. What is the benefit of using `this(...)` to delegate constructors?
IntermediateAnswers and explanations are in the Veda app.
Q112. What happens if you try to call `new Student()` after defining only `Student(String name)`?
IntermediateAnswers and explanations are in the Veda app.
Topper level · 24 questions
Q113. Which access modifier would you use for a field that should be accessible only to subclasses, even in different packages?
AdvancedAnswers and explanations are in the Veda app.
Q114. What is a potential drawback of using `protected` excessively?
AdvancedAnswers and explanations are in the Veda app.
Q115. Which of the following correctly describes the relationship between encapsulation and data hiding?
AdvancedAnswers and explanations are in the Veda app.
Q116. Consider the following class hierarchy: ```java package com.bank; public class Account { protected double balance; public Account(double initialBalance) { this.balance = initialBalance; } public double getBalance() { return balance; } } ``` ```java package com.bank; public class SavingsAccount extends Account { public SavingsAccount(double initialBalance) { super(initialBalance); } public void applyInterest(double rate) { // Assume rate is a percentage, e.g., 5 for 5% balance += balance * (rate / 100); } } ``` Now, a developer in a different package `com.app` writes: ```java package com.app; import com.bank.SavingsAccount; public class Main { public static void main(String[] args) { SavingsAccount sa = new SavingsAccount(1000); sa.applyInterest(5); System.out.println(sa.getBalance()); } } ``` What is the output and why?
AdvancedAnswers and explanations are in the Veda app.
Q117. You are designing a `User` class with a `password` field. Which of the following implementations best demonstrates the principle of least privilege and proper encapsulation, while still allowing password validation?
AdvancedAnswers and explanations are in the Veda app.
Q118. What is the result of the following code? ```java String s; System.out.println(s); ```
AdvancedAnswers and explanations are in the Veda app.
Q119. Consider the following code snippet: ```java class Counter { int count = 0; void increment() { count++; } } public class Main { public static void main(String[] args) { Counter a = new Counter(); Counter b = a; b.increment(); a.increment(); System.out.println(a.count + " " + b.count); } } ``` What is the output, and why?
AdvancedAnswers and explanations are in the Veda app.
Q120. Given the class definition: ```java class BankAccount { double balance; void deposit(double amount) { balance += amount; } void withdraw(double amount) { balance -= amount; } } ``` Which of the following code segments correctly creates two independent `BankAccount` objects and transfers $50 from the first to the second?
AdvancedAnswers and explanations are in the Veda app.
Q121. Consider the following code: ```java class Person { String name; Person(String n) { name = n; } void setName(String n) { name = n; } } public class Main { public static void main(String[] args) { Person p1 = new Person("Alice"); Person p2 = new Person("Bob"); p2 = p1; p2.setName("Charlie"); System.out.println(p1.name + " " + p2.name); } } ``` What is printed?
AdvancedAnswers and explanations are in the Veda app.
Q122. Which of the following statements correctly explains the behavior of the code below? ```java class Box { int size; Box(int s) { size = s; } } public class Main { public static void main(String[] args) { Box b1 = new Box(10); Box b2 = new Box(20); b1 = b2; b2.size = 30; System.out.println(b1.size); } } ```
AdvancedAnswers and explanations are in the Veda app.
Q123. A class has a static block and a static field initialized in the declaration. Which runs first?
AdvancedAnswers and explanations are in the Veda app.
Q124. Which of the following statements about static methods is true?
AdvancedAnswers and explanations are in the Veda app.
Q125. What is the purpose of a static block?
AdvancedAnswers and explanations are in the Veda app.
Q126. Which of the following is NOT a characteristic of static members?
AdvancedAnswers and explanations are in the Veda app.
Q127. What is the order of initialization of static fields and static blocks?
AdvancedAnswers and explanations are in the Veda app.
Q128. Consider the following Java code: ```java class Base { static int x = 10; static { x += 5; } static void show() { System.out.print("Base " + x + " "); } } class Derived extends Base { static int x = 20; static { x += 10; } static void show() { System.out.print("Derived " + x + " "); } } public class Test { public static void main(String[] args) { Base b = new Derived(); b.show(); System.out.print("Main " + Base.x + " " + Derived.x); } } ``` What is the output?
AdvancedAnswers and explanations are in the Veda app.
Q129. Which of the following statements about constructors is true?
AdvancedAnswers and explanations are in the Veda app.
Q130. In the context of object lifecycle, what is the role of the constructor?
AdvancedAnswers and explanations are in the Veda app.
Q131. What is the role of constructors in dependency injection?
AdvancedAnswers and explanations are in the Veda app.
Q132. Can a constructor throw an exception? Give an example.
AdvancedAnswers and explanations are in the Veda app.
Q133. What is the difference between a constructor and a method?
AdvancedAnswers and explanations are in the Veda app.
Q134. Consider the following Java class: ```java class Account { private double balance; private String owner; Account() { this("Unknown", 0.0); } Account(String owner) { this(owner, 0.0); } Account(String owner, double balance) { if (balance < 0) { throw new IllegalArgumentException("Balance cannot be negative"); } this.owner = owner; this.balance = balance; } } ``` What is the result of executing `new Account("Alice", -5.0)`?
AdvancedAnswers and explanations are in the Veda app.
Q135. Given the following class definitions: ```java class Parent { Parent() { System.out.print("Parent "); } } class Child extends Parent { private int value = 10; Child() { System.out.print("Child "); } } ``` What is the output when executing `new Child()`?
AdvancedAnswers and explanations are in the Veda app.
Q136. Consider the following class: ```java class Config { private static final int MAX = 100; private int limit; Config(int limit) { this.limit = limit; } Config() { this(MAX); } } ``` What happens if you attempt to compile and run `new Config()`?
AdvancedAnswers and explanations are in the Veda app.
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free to start, in more than 40 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Paper Solver
Get Veda free See what students get
Free to start · Android app and web · More than 40 languages