Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Document preview thumbnail
Preview 2 out of 6 pages
Exam (elaborations)

Java OOP Advanced Exam Questions and Answers — Inheritance, Polymorphism, Exceptions

Document preview thumbnail
Preview 2 out of 6 pages

Advanced Java OOP exam covering the most challenging topics tested in second-year Computer Science courses. 25 questions with detailed verified answers and explanations. Topics covered: - Constructor inheritance and polymorphism in superclass constructors - Abstract classes vs interfaces — when to use each - Method overriding with dynamic (runtime) binding - Static binding vs dynamic binding — which applies to static methods - ArrayList: removeAll(), equals(), remove(index) vs remove(value) - Exception handling: try/catch/finally, RuntimeException hierarchy - Short-circuit evaluation (&& and ||) - Immutable objects — requirements and implementation - Preventing instantiation with private constructors - instanceof across sibling classes - do-while loops, continue, and break behavior - Common Java pitfalls: = vs ==, post-increment in expressions Each question includes the correct answer plus an explanation of why wrong options fail — ideal for exam preparation.

Content preview

Java OOP Advanced
Inheritance, Polymorphism, Exceptions & Collections

Advanced Java | Undergraduate CS
Questions & Verified Answers

, Java OOP Advanced — Inheritance, Polymorphism &
Collections
25 questions covering advanced Java: inheritance, polymorphism, abstract classes, interfaces,
exception handling, and ArrayList operations.


Q1. How can you prevent a class from being instantiated in Java?
A. No modifiers on the constructor
B. Use private on the constructor
C. Use static on the constructor
D. Use public on the constructor
Answer: B. Use the private modifier on the constructor.
A private constructor prevents new ClassName() from being called outside the class.
Commonly used in: Singleton pattern, utility classes with only static methods.
Note: static constructors are not allowed in Java — option C is invalid syntax.
Q2. Analyze: class TempClass { int i; public void TempClass(int j){i=j;} } — new TempClass(2).
What happens?
A. Runs fine
B. Runtime error
C. Compile error
D. Works but i is 0
Answer: C. Compile error — TempClass does not have a constructor with an int argument.
'public void TempClass(int j)' has a return type (void) → it is a METHOD, not a constructor.
A constructor has NO return type. Since no constructor is defined, Java provides only a no-arg
default.
Fix: remove 'void': public TempClass(int j) { i = j; }
Q3. What is the output when new B() is called, given: class A has a constructor that calls setI(20)
and prints 'i from A is '+i, and class B overrides setI to set i = 3*i?
A. i from A is 7
B. i from A is 60
C. i from A is 20
D. i from A is 0
Answer: B. i from A is 60
When new B() runs, A's constructor is called first.
Inside A's constructor, setI(20) is called. Due to polymorphism, B's overridden setI runs.
B's setI: i = 3 × 20 = 60. Then A's constructor prints: i from A is 60.
Key lesson: overridden methods are called polymorphically even inside superclass constructors.
Q4. When does polymorphism (dynamic binding) NOT apply in Java?
A. Overridden instance methods
B. Static methods
C. Abstract methods
D. Interface methods

Document information

Uploaded on
May 13, 2026
Number of pages
6
Written in
2025/2026
Type
Exam (elaborations)
Contains
Questions & answers
$14.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
1
Followers
0
Items
38
Last sold
2 months ago


Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions