Adv-Java Unit 1: Questions & Answers
Unit 1: Object-Oriented Programming Concepts, Enumerations, Autoboxing, Annotations & Collections Framework -> Generated and Prepared By Thiruselvan (ThiruXD)
1. MULTIPLE CHOICE QUESTIONS (MCQs)
Easy:
1. Which of the following is not a pillar of OOP?
a) Encapsulation
b) Inheritance
c) Compilation
d) Polymorphism
Ans: c) Compilation
2. Which keyword is used to inherit a class in Java?
a) implements
b) extends
c) inherits
d) super
Ans: b) extends
3. A constructor has:
a) Return type void
b) Same name as class and no return type
c) Any name
d) Must return int
Ans: b) Same name as class and no return type
4. Which access modifier provides the highest level of encapsulation?
a) public
b) protected
c) default
d) private
Ans: d) private
5. What is the default value of a boolean variable?
a) true
b) 0
c) false
d) null
Ans: c) false
Medium
6. Which of the following is true about Method Overloading?
a) Same method name + same parameters
b) Same method name + different parameters
c) Different method name + same parameters
d) Only return type changes
Ans: b) Same method name + different parameters
7. Which statement is correct about abstract classes?
a) Can be instantiated
b) Can have both abstract and concrete methods
c) Cannot have constructors
d) Must contain only abstract methods
Ans: b) Can have both abstract and concrete methods
8. What does the values() method of an enum return?
a) String representation
b) Array of all enum constants
c) Ordinal number
d) Single constant
Ans: b) Array of all enum constants
9. Autoboxing converts:
a) Wrapper → Primitive
b) Primitive → Wrapper
c) String → int
d) Object → String
Ans: b) Primitive → Wrapper
10. Which collection does not allow duplicate elements?
a) ArrayList
b) LinkedList
c) HashSet
d) Vector
Ans: c) HashSet
Hard
11. Which of the following is true?
a) Java supports multiple inheritance of classes
b) An interface can extend multiple interfaces
c) Abstract class can be final
d) Constructor can be abstract
Ans: b) An interface can extend multiple interfaces
12. What will be the result of comparing two Integer objects created with values 200 using ==?
a) true
b) false
c) Compile-time error
d) Runtime error
Ans: b) false
(Integer cache is only for –128 to 127)
13. Which annotation is used to ensure a method is overriding a parent method?
a) @Deprecated
b) @Override
c) @SuppressWarnings
d) @FunctionalInterface
Ans: b) @Override
14. Which of the following statements about valueOf() is correct?
a) It is case-insensitive
b) Returns null if constant not found
c) Throws IllegalArgumentException if name not found
d) Returns ordinal value
Ans: c) Throws IllegalArgumentException if name not found
15. Which collection maintains insertion order and allows duplicates?
a) HashSet
b) TreeSet
c) ArrayList
d) HashMap
Ans: c) ArrayList
2. THEORY QUESTIONS
Easy
Q1. Define Class and Object.
Ans:
- Class: A blueprint or template that defines data members (state) and methods (behaviour).
- Object: A concrete instance of a class created in memory using the
newkeyword.
Q2. What is Encapsulation? Give one advantage.
Ans: Encapsulation is the process of wrapping data and methods into a single unit and restricting direct access to data using access modifiers.
Advantage: Data security and validation can be enforced through setters.
Q3. What is the purpose of a constructor?
Ans: A constructor is used to initialize the state of an object at the time of its creation.
Q4. List any four features of Java.
Ans: Platform independent, Object-oriented, Secure, Robust, Multithreaded, Portable (any four).
Medium
Q5. Differentiate between Method Overloading and Method Overriding.
| Point | Overloading | Overriding |
|---|---|---|
| Binding | Compile-time (Static) | Runtime (Dynamic) |
| Class | Same class | Inheritance (Parent-Child) |
| Parameters | Must differ | Must be same |
| Return type | Can be different | Must be same (or covariant) |
| Access modifier | Can change | Cannot be more restrictive |
Q6. Explain the difference between Abstract Class and Interface.
| Feature | Abstract Class | Interface |
|---|---|---|
| Keyword | abstract class | interface |
| Multiple inheritance | Not supported | Supported |
| Methods | Abstract + concrete | Abstract + default/static (Java 8+) |
| Fields | Any type | public static final only |
| Constructor | Allowed | Not allowed |
Q7. What is Autoboxing and Unboxing? Why are they required?
Ans:
- Autoboxing: Automatic conversion of primitive type to its corresponding wrapper class.
- Unboxing: Automatic conversion of wrapper class object to its primitive type.Need: Collections and Generics work only with objects, not primitives.
Q8. Explain the values() and valueOf() methods of Enum.
Ans:
values()→ Returns an array containing all constants of the enum in the order they are declared.valueOf(String name)→ Returns the enum constant whose name exactly matches the given string (case-sensitive). ThrowsIllegalArgumentExceptionif not found.
Hard
Q9. Why does Java not support multiple inheritance of classes? How is it achieved?
Ans: Multiple inheritance of classes can lead to the diamond problem (ambiguity when two parent classes have the same method).
Java avoids this by allowing a class to extend only one class. Multiple inheritance of type is achieved using interfaces.
Q10. Explain Dynamic Method Dispatch with an example.
Ans: Dynamic Method Dispatch is the mechanism by which a call to an overridden method is resolved at runtime based on the actual object type rather than the reference type.
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Bark"); }
}
Animal a = new Dog();
a.sound(); // Output: Bark (resolved at runtime)Q11. What are the retention policies of annotations? Explain.
Ans:
- SOURCE → Discarded by the compiler (not present in .class file).
- CLASS → Retained in .class file but not available at runtime.
- RUNTIME → Available at runtime through reflection.
Q12. Differentiate between ArrayList and LinkedList.
| Feature | ArrayList | LinkedList |
|---|---|---|
| Structure | Dynamic array | Doubly linked list |
| Access | Fast (O(1)) | Slow (O(n)) |
| Insertion/Deletion | Slow in middle | Fast |
| Memory | Less overhead | More overhead (pointers) |
| Best for | Frequent random access | Frequent insertions/deletions |
3. ANALYTICAL / CODE-BASED QUESTIONS
Easy
Q1. Predict the output:
class Test {
public static void main(String[] args) {
int x = 10;
Integer y = x; // autoboxing
System.out.println(y);
}
}Ans: 10
Q2. What will be the output?
enum Color { RED, GREEN, BLUE }
public class Demo {
public static void main(String[] args) {
System.out.println(Color.GREEN.ordinal());
}
}Ans: 1
Medium
Q3. Predict the output:
class Parent {
void show() { System.out.println("Parent"); }
}
class Child extends Parent {
void show() { System.out.println("Child"); }
}
public class Test {
public static void main(String[] args) {
Parent p = new Child();
p.show();
}
}Ans: Child
(Runtime polymorphism / Dynamic method dispatch)
Q4. Find the error (if any):
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
// no method
}Ans: Error – Circle must implement the abstract method draw() or itself be declared abstract.
Q5. What will happen?
Integer a = 100;
Integer b = 100;
Integer c = 200;
Integer d = 200;
System.out.println(a == b);
System.out.println(c == d);Ans:
true
false
(Integer cache range is –128 to 127)
Hard
Q6. Predict the complete output:
enum Level { LOW, MEDIUM, HIGH }
public class Test {
public static void main(String[] args) {
for (Level l : Level.values()) {
System.out.print(l + " ");
}
System.out.println();
Level x = Level.valueOf("MEDIUM");
System.out.println(x);
System.out.println(x.ordinal());
}
}Ans:
LOW MEDIUM HIGH
MEDIUM
1Q7. Analytical Question:
A developer writes the following code:
List<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
int sum = 0;
for (int n : list) {
sum += n;
}Explain step-by-step what happens regarding Autoboxing and Unboxing.
Ans:
list.add(10)→ Autoboxing convertsint 10toIntegerobject.- Same for 20.
- In the enhanced for-loop, each
Integeris unboxed tointso that it can be added to the primitive variablesum.
Q8. Code Writing (Analytical):
Write a Java program to demonstrate:
- An abstract class
Vehiclewith an abstract methodstart() - A subclass
Carthat overridesstart() - Use of runtime polymorphism
Sample Answer:
abstract class Vehicle {
abstract void start();
}
class Car extends Vehicle {
void start() {
System.out.println("Car started with key");
}
}
public class Demo {
public static void main(String[] args) {
Vehicle v = new Car(); // upcasting
v.start(); // Dynamic method dispatch
}
}Q9. Hard Analytical:
Why does the following code throw NullPointerException?
Integer num = null;
int x = num;Ans: Unboxing of a null Integer reference tries to call num.intValue(), which results in NullPointerException.
Q10. Design Question:
How would you model the following using Enum and Collections?
“A university has three result categories: FCD, FC, SC. Store the number of students in each category and print them.”
Sample Approach:
enum Result { FCD, FC, SC }
Map<Result, Integer> map = new HashMap<>();
map.put(Result.FCD, 45);
map.put(Result.FC, 30);
map.put(Result.SC, 25);
for (Result r : Result.values()) {
System.out.println(r + " : " + map.get(r));
}Quick Revision Tips for Exam
- Always remember: Overloading = Compile-time, Overriding = Runtime
- Abstract class can have constructor; Interface cannot
- Enum constants are
public static final - Integer cache → –128 to 127
- Prefer
.equals()over==for objects - List → allows duplicates | Set → no duplicates | Map → key-value
Adv-Java Unit 1: Complete Concept Guide
Unit 1: Object-Oriented Programming Concepts, Enumerations, Autoboxing, Annotations & Collections Framework -> Generated and Prepared By Thiruselvan (ThiruXD)
Adv-Java Unit 2: Complete Concept Guide
Unit 2: Servlets • JSP • JSTL -> Generated and Prepared By Thiruselvan (ThiruXD)