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?

Beginner
A `protected`
B `private`
C `public`
D package-private

Answers and explanations are in the Veda app.

Q2. What is the default access level in Java when no modifier is written?

Beginner
A `private`
B package-private
C `protected`
D `public`

Answers and explanations are in the Veda app.

Q3. Which access modifier allows access within the same package and to subclasses in other packages?

Beginner
A package-private
B `public`
C `protected`
D `private`

Answers and explanations are in the Veda app.

Q4. What is the main benefit of data hiding?

Beginner
A It makes fields public
B It makes code longer
C It allows any class to access fields
D It prevents external code from setting invalid values

Answers and explanations are in the Veda app.

Q5. Which of the following is NOT a benefit of encapsulation?

Beginner
A Flexibility
B Increased execution speed
C Maintainability
D Data hiding

Answers 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.

Beginner
A True
B False

Answers and explanations are in the Veda app.

Q7. What is encapsulation in Java?

Beginner

Answers and explanations are in the Veda app.

Q8. List the four access levels in Java from most restrictive to least restrictive.

Beginner

Answers and explanations are in the Veda app.

Q9. What is the visibility of a `private` member?

Beginner

Answers and explanations are in the Veda app.

Q10. What is the default access level in Java when no modifier is specified?

Beginner

Answers and explanations are in the Veda app.

Q11. What is the visibility of a `protected` member?

Beginner

Answers and explanations are in the Veda app.

Q12. What is the visibility of a `public` member?

Beginner

Answers and explanations are in the Veda app.

Q13. What is a getter method?

Beginner

Answers and explanations are in the Veda app.

Q14. What is a setter method?

Beginner

Answers and explanations are in the Veda app.

Q15. Name two benefits of encapsulation.

Beginner

Answers and explanations are in the Veda app.

Q16. What is a common pattern that uses encapsulation in Java?

Beginner

Answers and explanations are in the Veda app.

Q17. What is a class in Java?

Beginner
A A concrete instance of data
B A blueprint for creating objects
C A variable that stores a value
D A method that performs an action

Answers and explanations are in the Veda app.

Q18. Which keyword is used to create an object in Java?

Beginner
A new
B create
C class
D object

Answers and explanations are in the Veda app.

Q19. What does a reference variable store?

Beginner
A The class name
B A memory address of the object
C The object's methods
D The actual object data

Answers and explanations are in the Veda app.

Q20. Which of the following correctly creates a new `Dog` object?

Beginner
A Dog d = create Dog();
B new Dog d;
C Dog d = new Dog();
D Dog d = Dog();

Answers and explanations are in the Veda app.

Q21. Which of the following is a primitive type in Java?

Beginner
A Scanner
B String
C Car
D int

Answers and explanations are in the Veda app.

Q22. What does the dot operator (`.`) do in Java?

Beginner
A It compares two objects
B It creates a new object
C It declares a variable
D It accesses fields and methods of an object

Answers and explanations are in the Veda app.

Q23. If a class has no constructor defined, what happens?

Beginner
A The object is created without initialization
B The class cannot be instantiated
C A default constructor is provided
D A compile-time error occurs

Answers and explanations are in the Veda app.

Q24. A reference variable stores the actual object data.

Beginner
A True
B False

Answers and explanations are in the Veda app.

Q25. Declaring a reference variable creates an object in memory.

Beginner
A True
B False

Answers and explanations are in the Veda app.

Q26. What is a class in Java?

Beginner

Answers and explanations are in the Veda app.

Q27. What does the `new` keyword do?

Beginner

Answers and explanations are in the Veda app.

Q28. How do you access fields and methods of an object?

Beginner

Answers and explanations are in the Veda app.

Q29. What is the default value of a reference variable that has not been assigned an object?

Beginner

Answers and explanations are in the Veda app.

Q30. What exception is thrown when you call a method on a `null` reference?

Beginner

Answers and explanations are in the Veda app.

Q31. What is object state?

Beginner

Answers and explanations are in the Veda app.

Q32. What is object behavior?

Beginner

Answers and explanations are in the Veda app.

Q33. If no constructor is defined in a class, what happens?

Beginner

Answers and explanations are in the Veda app.

Q34. Do two objects of the same class share their instance fields?

Beginner

Answers and explanations are in the Veda app.

Q35. What is the result of `Car myCar;` without `new`?

Beginner

Answers and explanations are in the Veda app.

Q36. What is the purpose of the dot operator?

Beginner

Answers and explanations are in the Veda app.

Q37. Which of the following best describes a static field?

Beginner
A A variable that is automatically final
B A variable that can only be accessed within the class
C A variable shared by all instances of the class
D A variable that belongs to each object separately

Answers 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`?

Beginner
A `foo()` directly
B `new MyClass().foo()`
C `MyClass.new foo()`
D `MyClass.foo()`

Answers and explanations are in the Veda app.

Q39. When is a static block executed?

Beginner
A Every time an object is created
B When the class is first loaded
C When the program terminates
D When the first static method is called

Answers and explanations are in the Veda app.

Q40. Which of the following is a correct declaration of a static constant?

Beginner
A `public final int MAX = 10;`
B `public static final int MAX = 10;`
C `static final MAX = 10;`
D `public static int MAX = 10;`

Answers and explanations are in the Veda app.

Q41. A static block executes every time a new object of the class is created.

Beginner
A True
B False

Answers and explanations are in the Veda app.

Q42. What is the difference between a static field and an instance field?

Beginner

Answers and explanations are in the Veda app.

Q43. Why is the `main` method declared static?

Beginner

Answers and explanations are in the Veda app.

Q44. What is a static block and when does it run?

Beginner

Answers and explanations are in the Veda app.

Q45. Give an example of a static constant and explain why it is useful.

Beginner

Answers and explanations are in the Veda app.

Q46. Can a static method be called using an object reference? Is it recommended?

Beginner

Answers 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?

Beginner

Answers and explanations are in the Veda app.

Q48. What is the purpose of a static initializer block?

Beginner

Answers and explanations are in the Veda app.

Q49. Can a static method access another static method directly?

Beginner

Answers and explanations are in the Veda app.

Q50. What is the primary purpose of a constructor in Java?

Beginner
A To allocate memory for the object
B To initialize a new object's state when it is created
C To define the class's methods
D To destroy objects when they are no longer needed

Answers and explanations are in the Veda app.

Q51. If a class defines no constructors, what does Java provide?

Beginner
A A default no-argument constructor
B A private constructor
C A parameterized constructor with all fields
D No constructor at all

Answers and explanations are in the Veda app.

Q52. What is the effect of defining any constructor in a class?

Beginner
A The default constructor disappears
B The default constructor is still provided
C The class cannot be instantiated
D The class becomes abstract

Answers and explanations are in the Veda app.

Q53. Which of the following correctly describes a parameterized constructor?

Beginner
A It has no parameters and sets default values
B It is automatically generated by the compiler
C It accepts arguments to initialize an object with specific values
D It can only be called once per object

Answers and explanations are in the Veda app.

Q54. What is constructor overloading?

Beginner
A Having a constructor that calls a method
B Having multiple constructors with different parameter lists
C Having a constructor with a return type
D Having multiple constructors with the same parameter list

Answers and explanations are in the Veda app.

Q55. What does the `this` keyword refer to in a constructor?

Beginner
A The superclass
B The current instance of the class
C The class itself
D The constructor's return value

Answers and explanations are in the Veda app.

Q56. The default constructor is provided by the compiler only if the class has no other constructors.

Beginner
A True
B False

Answers and explanations are in the Veda app.

Q57. A constructor can be called using dot notation on an existing object.

Beginner
A True
B False

Answers and explanations are in the Veda app.

Q58. What is a constructor in Java?

Beginner

Answers and explanations are in the Veda app.

Q59. What does the default constructor do?

Beginner

Answers and explanations are in the Veda app.

Q60. When does the default constructor disappear?

Beginner

Answers and explanations are in the Veda app.

Q61. What is a parameterized constructor?

Beginner

Answers and explanations are in the Veda app.

Q62. What is constructor overloading?

Beginner

Answers and explanations are in the Veda app.

Q63. What does the `this` keyword refer to in a constructor?

Beginner

Answers 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?

Intermediate
A To make code run faster
B To make fields visible to subclasses
C To enforce encapsulation and control access
D To allow direct field access from anywhere

Answers 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?

Intermediate
A `public double getBalance() { return balance; }`
B `public void withdraw(double amount) { if (amount > 0 && amount <= balance) balance -= amount; }`
C `public void withdraw(double amount) { balance -= amount; }`
D `public void setBalance(double b) { balance = b; }`

Answers and explanations are in the Veda app.

Q66. Why does encapsulation reduce coupling between classes?

Intermediate
A Because external code depends on the public interface, not internal details
B Because all fields are public
C Because classes become larger
D Because classes cannot interact

Answers and explanations are in the Veda app.

Q67. Which of the following is a common mistake that breaks encapsulation?

Intermediate
A Providing public getters and setters
B Making fields private
C Making fields public to avoid writing getters/setters
D Using `protected` for fields

Answers and explanations are in the Veda app.

Q68. What is the principle of least privilege in the context of access modifiers?

Intermediate
A Use `protected` for all fields
B Use the most restrictive access that works
C Use `public` for everything
D Use package-private for all methods

Answers and explanations are in the Veda app.

Q69. In the context of encapsulation, what does a setter method typically do besides updating the field?

Intermediate
A It makes the field public
B It validates the input
C It returns the field value
D It deletes the field

Answers and explanations are in the Veda app.

Q70. Why does encapsulation improve maintainability?

Intermediate
A It makes code longer
B It prevents any changes
C It isolates internal changes from external code
D It makes all fields public

Answers and explanations are in the Veda app.

Q71. Encapsulation is the same as data hiding.

Intermediate
A True
B False

Answers and explanations are in the Veda app.

Q72. How do you enforce encapsulation for a field?

Intermediate

Answers and explanations are in the Veda app.

Q73. What is the principle of least privilege in access control?

Intermediate

Answers and explanations are in the Veda app.

Q74. What is the relationship between encapsulation and data hiding?

Intermediate

Answers and explanations are in the Veda app.

Q75. What happens if you make a field public?

Intermediate

Answers and explanations are in the Veda app.

Q76. Why does encapsulation improve maintainability?

Intermediate

Answers and explanations are in the Veda app.

Q77. What happens when you assign `obj2 = obj1;` where both are reference variables of the same type?

Intermediate
A A compile-time error occurs
B Both variables point to the same object
C The object is copied to obj2
D obj2 becomes null

Answers and explanations are in the Veda app.

Q78. What is the primary difference between a class and an object?

Intermediate
A They are the same thing
B An object is a blueprint; a class is an instance
C A class is a blueprint; an object is an instance
D A class holds data; an object holds methods

Answers and explanations are in the Veda app.

Q79. What is the state of an object?

Intermediate
A The values of its fields at a given time
B The class it belongs to
C The memory address it occupies
D The methods it can perform

Answers and explanations are in the Veda app.

Q80. What happens if you call a method on a `null` reference?

Intermediate
A The method returns null
B A compile-time error occurs
C The program continues silently
D A NullPointerException is thrown

Answers and explanations are in the Veda app.

Q81. Which of the following correctly describes the behavior of an object?

Intermediate
A It is defined by its fields
B It is defined by its memory address
C It is defined by its methods
D It is defined by its class name

Answers and explanations are in the Veda app.

Q82. What is the purpose of a constructor?

Intermediate
A To initialize a new object
B To declare fields
C To destroy an object
D To define methods

Answers and explanations are in the Veda app.

Q83. Which of the following statements about instance fields is true?

Intermediate
A Instance fields are static
B Each object has its own copy of instance fields
C All objects of a class share the same instance fields
D Instance fields cannot be changed

Answers and explanations are in the Veda app.

Q84. What is the difference between a primitive type and a reference type?

Intermediate

Answers and explanations are in the Veda app.

Q85. What happens when you assign one reference variable to another?

Intermediate

Answers and explanations are in the Veda app.

Q86. What is a constructor?

Intermediate

Answers and explanations are in the Veda app.

Q87. Why are classes considered reusable?

Intermediate

Answers and explanations are in the Veda app.

Q88. Why can't a static method directly access an instance variable?

Intermediate
A Because static methods don't have a `this` reference
B Because instance variables are private
C Because static methods run before objects exist
D Because instance variables are not initialized yet

Answers 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?

Intermediate
A The parent method is hidden
B A compile-time error occurs
C The subclass method is ignored
D The parent method is overridden

Answers 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?

Intermediate
A Compilation error
B A
C B
D Runtime error

Answers and explanations are in the Veda app.

Q91. Which of the following is an appropriate use of a static method?

Intermediate
A A method that prints the name of an employee
B A method that calculates the area of a circle given its radius
C A method that updates the balance of a bank account
D A method that returns the color of a specific car object

Answers and explanations are in the Veda app.

Q92. What is the main advantage of using static constants?

Intermediate
A They are automatically public
B They allow method overriding
C They save memory and improve readability
D They can be changed at runtime

Answers and explanations are in the Veda app.

Q93. Static methods can be overridden in subclasses.

Intermediate
A True
B False

Answers and explanations are in the Veda app.

Q94. Can a static method access an instance variable? Explain.

Intermediate

Answers and explanations are in the Veda app.

Q95. What does it mean that static methods are 'hidden' rather than overridden?

Intermediate

Answers and explanations are in the Veda app.

Q96. Why are utility methods like `Math.sqrt()` static?

Intermediate

Answers and explanations are in the Veda app.

Q97. What is a common use of static fields for shared state?

Intermediate

Answers and explanations are in the Veda app.

Q98. Explain the difference between `static` and `final`.

Intermediate

Answers and explanations are in the Veda app.

Q99. How does the JVM call the `main` method if no object exists?

Intermediate

Answers and explanations are in the Veda app.

Q100. Which of the following is a valid use of `this` in a constructor?

Intermediate
A `this();` to call the superclass constructor
B `return this;` to return the object
C `this = new Student();`
D `this.name = name;` to disambiguate fields

Answers and explanations are in the Veda app.

Q101. What is the rule about `this(...)` in a constructor?

Intermediate
A It must be the last statement in the constructor
B It must be the first statement in the constructor
C It can only be used in the default constructor
D It can be placed anywhere in the constructor

Answers and explanations are in the Veda app.

Q102. What is the correct initialization order when creating an object?

Intermediate
A Explicit field initializers → memory allocation → default values → constructor body
B Constructor body → explicit field initializers → default values → memory allocation
C Memory allocation → default values → explicit field initializers → constructor body
D Default values → memory allocation → constructor body → explicit field initializers

Answers and explanations are in the Veda app.

Q103. What happens if you try to call `new Student()` after defining only `Student(String name)`?

Intermediate
A It compiles and uses default values
B It automatically calls the parameterized constructor with null
C It causes a compile error because the default constructor is gone
D It throws a runtime exception

Answers and explanations are in the Veda app.

Q104. Why might a constructor throw an exception like `IllegalArgumentException`?

Intermediate
A To handle memory allocation failures
B To signal that the constructor is overloaded
C To allow the object to be garbage collected
D To prevent invalid object creation and enforce invariants

Answers 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?

Intermediate
A It creates a student with null name and 0 age
B It delegates to the parameterized constructor to set default values
C It overrides the default constructor
D It is required by the compiler

Answers and explanations are in the Veda app.

Q106. What is the main benefit of using `this(...)` to delegate constructors?

Intermediate
A It improves memory allocation speed
B It reduces code duplication by centralizing initialization
C It allows calling superclass constructors
D It makes the class final

Answers and explanations are in the Veda app.

Q107. What is the rule about `this(...)` in a constructor?

Intermediate

Answers and explanations are in the Veda app.

Q108. What is the initialization order when creating an object?

Intermediate

Answers and explanations are in the Veda app.

Q109. Why do constructors have no return type?

Intermediate

Answers and explanations are in the Veda app.

Q110. How can a constructor enforce object invariants?

Intermediate

Answers and explanations are in the Veda app.

Q111. What is the benefit of using `this(...)` to delegate constructors?

Intermediate

Answers and explanations are in the Veda app.

Q112. What happens if you try to call `new Student()` after defining only `Student(String name)`?

Intermediate

Answers 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?

Advanced
A `public`
B `protected`
C package-private
D `private`

Answers and explanations are in the Veda app.

Q114. What is a potential drawback of using `protected` excessively?

Advanced
A It makes fields private
B It makes code too secure
C It exposes data to subclasses and same-package classes
D It prevents inheritance

Answers and explanations are in the Veda app.

Q115. Which of the following correctly describes the relationship between encapsulation and data hiding?

Advanced
A They are the same thing
B Data hiding is a subset of encapsulation
C They are unrelated
D Encapsulation is a subset of data hiding

Answers 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?

Advanced
A 1000.0, because `applyInterest` does not actually modify the balance due to encapsulation.
B 1050.0, because `balance` is `protected` and `SavingsAccount` is a subclass, so it can modify its own inherited field.
C Compilation error, because `getBalance()` is not accessible from `com.app` package.
D Compilation error, because `balance` is `protected` and cannot be accessed from a subclass in a different package.

Answers 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?

Advanced
A ```java public class User { private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } ```
B ```java public class User { private String password; public void setPassword(String password) { this.password = password; } public String getPasswordHash() { return Integer.toHexString(password.hashCode()); } } ```
C ```java public class User { private String password; public boolean validatePassword(String input) { return this.password.equals(input); } public void setPassword(String password) { if (password.length() >= 8) { this.password = password; } } } ```
D ```java public class User { public String password; public boolean validatePassword(String input) { return this.password.equals(input); } } ```

Answers and explanations are in the Veda app.

Q118. What is the result of the following code? ```java String s; System.out.println(s); ```

Advanced
A Prints null
B Prints empty string
C Compilation error: variable s might not be initialized
D Runtime exception

Answers 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?

Advanced
A `2 2` because `a` and `b` refer to the same object, so both see the incremented count.
B `0 0` because the `count` field is not accessible through reference variables.
C `1 1` because each reference variable maintains its own copy of the `count` field.
D `2 1` because `b.increment()` affects only `b`'s copy, while `a.increment()` affects only `a`'s copy.

Answers 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?

Advanced
A ```java BankAccount acc1 = new BankAccount(); BankAccount acc2 = new BankAccount(); acc1.deposit(100); acc1.withdraw(50); acc2.deposit(50); ```
B ```java BankAccount acc1 = new BankAccount(); BankAccount acc2 = new BankAccount(); acc1.deposit(100); acc1.withdraw(50); acc1.deposit(50); ```
C ```java BankAccount acc1 = new BankAccount(); BankAccount acc2 = acc1; acc1.deposit(100); acc1.withdraw(50); acc2.deposit(50); ```
D ```java BankAccount acc1 = new BankAccount(); BankAccount acc2 = new BankAccount(); acc1.deposit(100); acc2.withdraw(50); acc2.deposit(50); ```

Answers 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?

Advanced
A `Alice Charlie` because `p1` still points to the original object, while `p2` points to a new object.
B `Charlie Charlie` because `p2` now references the same object as `p1`, and the name is changed through that shared reference.
C `Alice Bob` because each object retains its own name.
D `null null` because the `setName` method does not work on reference variables.

Answers 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); } } ```

Advanced
A It prints `30` because after `b1 = b2;`, both variables refer to the same object, and modifying `b2.size` changes the shared object's field.
B It throws a `NullPointerException` because `b1` becomes `null` after the assignment.
C It prints `20` because `b1` was assigned the reference to the object with size 20, and that object's size hasn't changed.
D It prints `10` because `b1` still points to the original object with size 10.

Answers 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?

Advanced
A The static block always runs first
B The field initialization always runs first
C They run in the order they appear in the source code
D It depends on the JVM implementation

Answers and explanations are in the Veda app.

Q124. Which of the following statements about static methods is true?

Advanced
A They can use `this` to refer to the current object
B They are automatically synchronized
C They can be overridden in subclasses
D They can access instance variables via an object reference

Answers and explanations are in the Veda app.

Q125. What is the purpose of a static block?

Advanced
A To create multiple instances of the class
B To override static methods
C To initialize instance variables
D To perform one-time setup when the class is loaded

Answers and explanations are in the Veda app.

Q126. Which of the following is NOT a characteristic of static members?

Advanced
A They are automatically final
B They are shared across all objects
C They belong to the class, not instances
D They can be accessed without creating an object

Answers and explanations are in the Veda app.

Q127. What is the order of initialization of static fields and static blocks?

Advanced

Answers 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?

Advanced
A Derived 30 Main 15 30
B Derived 30 Main 15 20
C Base 15 Main 15 30
D Base 15 Main 15 20

Answers and explanations are in the Veda app.

Q129. Which of the following statements about constructors is true?

Advanced
A Constructors can have a return type like `void`
B Constructors are inherited by subclasses
C Constructors can throw exceptions to prevent invalid object creation
D Constructors can be called using dot notation on an existing object

Answers and explanations are in the Veda app.

Q130. In the context of object lifecycle, what is the role of the constructor?

Advanced
A It marks the object for garbage collection
B It defines the class's static methods
C It is the final step in the creation phase, making the object ready for use
D It allocates memory for the object on the stack

Answers and explanations are in the Veda app.

Q131. What is the role of constructors in dependency injection?

Advanced

Answers and explanations are in the Veda app.

Q132. Can a constructor throw an exception? Give an example.

Advanced

Answers and explanations are in the Veda app.

Q133. What is the difference between a constructor and a method?

Advanced

Answers 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)`?

Advanced
A It causes a compile-time error because the constructor call is invalid.
B It creates an `Account` object with owner "Alice" and balance 0.0.
C It creates an `Account` object with owner "Alice" and balance -5.0.
D It throws `IllegalArgumentException` because the balance is negative.

Answers 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()`?

Advanced
A `Child Parent`
B `Child`
C `Parent Child`
D `Parent`

Answers 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()`?

Advanced
A It compiles but throws a runtime exception because `MAX` is not initialized.
B It compiles and creates a `Config` object with `limit` set to 100.
C It fails to compile because `this(MAX)` must be the first statement, but it is not.
D It fails to compile because `MAX` is not accessible in the constructor.

Answers and explanations are in the Veda app.

Make your own

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