Kit Library / Computer Science / Object-Oriented Programming

📚 Chapter Learning Kit

Java OOP Essentials

English 4 topics 136 leveled MCQs 58 flashcards 11 games Free

Shared by a Veda teacher · Generated with Veda AI

Veda Bites

The whole idea, one bite at a time

Veda Bites are swipeable micro-lessons — each one teaches exactly one idea. The bites below come from this kit — swipe through them in the app.

💡 Key Idea

Class vs Object: Blueprint vs Building

A class is a blueprint; an object is the building.

A class defines the structure (fields) and behavior (methods) that objects of that type will have. An object is a concrete instance created from that blueprint, with its own state.

↳ A class is a template; an object is a living instance with its own data.

📖 Definition

Fields and Methods: The Anatomy of a Class

Fields hold data; methods define actions.

↳ Fields represent what an object knows; methods represent what it does.

💻 Code Example

Defining a Class and Creating an Object

See a complete Java class in action.

↳ Use 'new' to create an object, then access its fields and methods with dot notation.

⚠️ Common Mistake

Primitive vs Reference: The '==' Trap

Comparing objects with '==' can mislead you.

↳ For objects, '==' tests identity, not equality — use '.equals()' for value comparison.

🌍 Real World

Objects in the Real World: A Bank Account

Your bank account is an object with state.

↳ OOP lets you map real-world entities to code, with each object holding its own state.

⚖️ Comparison

Primitive vs Reference Types

Two kinds of variables: store value or store address.

↳ Primitives hold values directly; references hold addresses to objects in memory.

✅ Best Practice

Naming Conventions: Clarity Matters

Names should reveal intent instantly.

↳ Consistent naming makes code readable and maintainable.

🧠 Memory Trick

Remember: 'new' Creates, Dot Accesses

New makes it, dot uses it.

Think of `new` as the constructor that brings an object to life, and the dot (`.`) as the remote control to access its fields and methods.

↳ Always use 'new' to instantiate, and dot notation to interact with the object.

🎯 Exam Tip

Spot the Reference Variable in Exams

Exams love testing reference vs primitive.

↳ Reference variables share the same object; changes through one are visible through all.

❓ Did You Know

Objects Live on the Heap

Objects reside in a special memory area.

In Java, all objects are allocated on the heap, while primitive variables and references can live on the stack. The garbage collector automatically cleans up objects no longer referenced.

↳ Java manages memory for objects via the heap and garbage collection.

💡 Key Idea

Constructors: The Object Initializers

Every object starts with a constructor call.

A constructor is a special method that initializes a new object when it is created. It has the same name as the class and no return type.

↳ Constructors are the entry point for object initialization, ensuring objects start in a valid state.

📖 Definition

Default vs. Parameterized Constructors

Two ways to build an object.

↳ Default constructors give basic initialization; parameterized constructors allow custom initial states.

💻 Code Example

Writing Default and Parameterized Constructors

See both constructors in action.

↳ Default constructors set safe fallback values; parameterized constructors set specific values at creation.

💡 Key Idea

Constructor Overloading: Multiple Ways to Initialize

Same name, different parameters.

Constructor overloading means defining multiple constructors in the same class, each with a different parameter list. Java picks the right one based on the arguments passed.

↳ Overloading gives flexibility: you can create objects with varying levels of detail.

💻 Code Example

Overloading Constructors in Practice

Multiple constructors, one class.

↳ Overloading lets you create objects with default, square, or custom dimensions — all using the same class name.

💡 Key Idea

The 'this' Keyword: Referring to the Current Instance

Disambiguate fields from parameters.

Inside a constructor, `this` refers to the current object being created. It's used to distinguish instance variables from parameters with the same name.

↳ `this` clarifies which variable you're referring to and enables constructor chaining.

💻 Code Example

Using 'this' to Resolve Name Conflicts

Same name? Use 'this'.

↳ `this` is essential when parameter names shadow field names, making code clear and error-free.

⚙️ Process

Initialization Order in an Object's Lifecycle

What runs first when you create an object?

↳ Fields get default values before the constructor body runs, so you can rely on them being set.

⚠️ Common Mistake

Forgetting the Default Constructor When Overloading

Overloaded? Default disappears.

↳ Once you add a parameterized constructor, the default is gone — plan accordingly.

⚠️ Common Mistake

Misusing 'this()' in Constructors

Constructor chaining has rules.

↳ Constructor chaining via `this()` is powerful but must follow the first-statement rule.

🎯 Exam Tip

Spotting Constructor Signatures in Exams

Look for the missing return type.

↳ In exam questions, identify constructors by the class name and absence of a return type.

🌍 Real World

Real-World Use: Configuring Objects with Constructors

How apps use constructors daily.

↳ Constructors are used everywhere to set up objects with required data and valid initial conditions.

💡 Key Idea

Access Modifiers Control Visibility

Who can see your class members?

Access modifiers in Java determine where a class, method, or field can be accessed from. They are the foundation of encapsulation, protecting data from unintended external changes.

↳ Choosing the right access modifier controls who can see and modify your code.

📖 Definition

The Four Access Levels

Know your four visibility options.

↳ Each modifier gives a different level of access, from most restrictive (private) to least (public).

⚖️ Comparison

Private vs. Public: The Core Trade-off

Why not make everything public?

↳ Private protects data; public exposes functionality. Use private by default for fields.

✏️ Example

Encapsulation in Action

A real-world bank account example.

↳ Encapsulation ensures that an object's data can only be changed in controlled ways.

💻 Code Example

Getters and Setters in Java

The standard way to protect fields.

↳ Getters and setters allow controlled access to private fields, enabling validation and future changes.

⚠️ Common Mistake

Making Fields Public for Convenience

Shortcut that breaks encapsulation.

↳ Public fields are a common anti-pattern; use private fields with accessors.

✅ Best Practice

Default to Private, Then Widen

Start restrictive, open up as needed.

↳ The principle of least privilege: give each member the minimum access it needs.

⭐ Important Fact

Benefits of Data Hiding

Why hiding data is a superpower.

↳ Data hiding improves reliability and maintainability of your code.

Smart notes

What you'll study, topic by topic

1

Classes, Objects, and Reference Variables in Java

15 min

This topic introduces the core building blocks of Java OOP: defining classes with fields and methods, creating objects with the 'new' keyword, and understanding how reference variables differ from primitive types. It est…

  • A class is a blueprint that defines fields (data) and methods (behavior).
  • An object is a concrete instance of a class, created with the `new` keyword.
  • The `new` keyword allocates memory and calls a constructor to initialize the object.

Full explanation, worked examples & memory tricks in the app

2

Constructors in Java: Types, Overloading, and the 'this' Keyword

12 min

This topic covers the role of constructors in Java object lifecycle, including default and parameterized constructors, constructor overloading, and the use of the 'this' keyword. It also explains initialization order and…

  • A constructor is a special method that initializes a new object's state when it is created with `new`.
  • If no constructor is defined, Java provides a default no-argument constructor that sets fields to default values (0, false, null).
  • Defining any constructor removes the default constructor; you must explicitly add a no-argument constructor if needed.

Full explanation, worked examples & memory tricks in the app

3

Access Modifiers and Encapsulation in Java

15 min

This topic covers Java's access modifiers (public, private, protected, package-private) and how they enforce encapsulation. It explains the benefits of data hiding and maintainability, with practical examples and common…

  • Encapsulation bundles data and methods into a class and restricts direct access to fields.
  • Access modifiers control visibility: private, package-private (default), protected, and public.
  • Private fields are accessible only within the same class.

Full explanation, worked examples & memory tricks in the app

4

Static vs Instance Context in Java

15 min

This topic explains the static keyword in Java, covering static fields, methods, blocks, and constants. It clarifies the difference between static (class-level) and instance (object-level) context, and when to use each a…

  • `static` members belong to the class, not to any instance; they are shared across all objects.
  • Static fields are class variables; only one copy exists in memory regardless of how many objects are created.
  • Static methods can be called using the class name without creating an object.

Full explanation, worked examples & memory tricks in the app

Leveled MCQ practice

Try the smart MCQs from this kit

120 of this kit's 136 questions, laddered from warm-up to topper-level, each with its answer and the reasoning. The rest are in the app.

Warm-up · 59 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.

Building up · 43 questions

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

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

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

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

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

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

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

Q67. Encapsulation is the same as data hiding.

Intermediate
A True
B False

Answers and explanations are in the Veda app.

Q68. How do you enforce encapsulation for a field?

Intermediate

Answers and explanations are in the Veda app.

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

Intermediate

Answers and explanations are in the Veda app.

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

Intermediate

Answers and explanations are in the Veda app.

Q71. What happens if you make a field public?

Intermediate

Answers and explanations are in the Veda app.

Q72. Why does encapsulation improve maintainability?

Intermediate

Answers and explanations are in the Veda app.

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

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

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

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

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

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

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

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

Intermediate

Answers and explanations are in the Veda app.

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

Intermediate

Answers and explanations are in the Veda app.

Q82. What is a constructor?

Intermediate

Answers and explanations are in the Veda app.

Q83. Why are classes considered reusable?

Intermediate

Answers and explanations are in the Veda app.

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

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

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

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

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

Q89. Static methods can be overridden in subclasses.

Intermediate
A True
B False

Answers and explanations are in the Veda app.

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

Intermediate

Answers and explanations are in the Veda app.

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

Intermediate

Answers and explanations are in the Veda app.

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

Intermediate

Answers and explanations are in the Veda app.

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

Intermediate

Answers and explanations are in the Veda app.

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

Intermediate

Answers and explanations are in the Veda app.

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

Intermediate

Answers and explanations are in the Veda app.

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

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

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

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

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

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

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

Topper level · 18 questions

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Advanced

Answers and explanations are in the Veda app.

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

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

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

Flashcards

Tap a card to flip it

58 flashcards in this kit — the app reviews them with spaced repetition so the right card returns on the right day.

Learning games

Play your way through this kit

Every game is built from this kit's own content — scores feed your mastery, so playing counts as studying.

🎮

Word Match

Built from this kit's own content

🎮

True False

Built from this kit's own content

🎮

Fill Blank

Built from this kit's own content

🎮

Memory Match

Built from this kit's own content

🎮

Flashcard Battle

Built from this kit's own content

🎮

Speed Quiz

Built from this kit's own content

🎮

Fact Or Myth

Built from this kit's own content

🎮

Guess Term

Built from this kit's own content

🎮

Sequence Builder

Built from this kit's own content

🎮

Categorization

Built from this kit's own content

🎮

Revision Battle

Built from this kit's own content

Playable in the app

Study it properly — free, in the app

The full Veda Bites deck, complete notes, spaced-repetition flashcards, leveled MCQs, tests and games for this kit — plus Daily Facts and the Arena, every day.