JAVA-OOP-Mock-Answers
Tags: #◇CCPROG3 #◇Object-Oriented Concepts #◇Programming #◇Computer Science #◇Java #◇Object-Oriented Programming #◇Software Engineering Principles #◇Algorithms
◇Overview
- C. Object-Oriented Programming
◇Details / Theory
◇Part I: Multiple Choice (Theory & Concepts)
-
C. Object-Oriented Programming
Explanation: OOP focuses on objects (entities/nouns) that encapsulate data and behavior, whereas Procedural focuses on functions (actions/verbs). -
B. Stack
Explanation: Primitive types (int, double, etc.) are stored directly in the Stack memory. Objects are stored in the Heap. -
C. null
Explanation: The default value for any object reference (String, Arrays, Classes) isnull. -
C. final
Explanation: Thefinalkeyword makes a variable constant, a method non-overridable, or a class non-inheritable. -
C. It can be called without creating an object of the class.
Explanation: Static members belong to the class itself, not any specific instance. -
C. Encapsulation
Explanation: Encapsulation is about bundling data and methods and restricting access (hiding details) via access modifiers. -
B. private
Explanation: Fields should beprivateto enforce encapsulation, accessible only via public getters/setters. -
D. It has no return type
Explanation: Constructors look like methods but have NO return type, not evenvoid. -
C. Aggregation
Explanation: Aggregation is a "Has-a" relationship where the child can exist independently (Weak coupling). -
B. Composition
Explanation: Composition is a strong "Part-of" relationship where the child dies if the parent dies. -
C. Set
Explanation: TheSetinterface (e.g.,HashSet) strictly prohibits duplicate elements. -
B. LinkedList
Explanation:LinkedListis faster for insertion/deletion () compared toArrayList() because it doesn't need to shift elements. -
C. Autoboxing
Explanation: Java automatically converts primitives to their wrapper classes (e.g.,inttoInteger). -
C. extends
Explanation: The keywordextendsis used for class inheritance. -
A. One
Explanation: Java supports Single Inheritance only for classes. A class can extend only one parent. -
B. Calls the superclass's constructor.
Explanation:super()invokes the constructor of the parent class and must be the first line in the child constructor. -
C. Compile-time Polymorphism
Explanation: Method Overloading is resolved at compile-time (static binding). -
C.
equals()
Explanation:==compares memory addresses.equals()is used to compare the actual content/values of objects. -
C. It causes a Compilation Error.
Explanation: Abstract classes cannot be instantiated directly usingnew. -
C. A class can implement multiple interfaces.
Explanation: While classes allow single inheritance, they can implement multiple interfaces. -
B. implements
Explanation: The keywordimplementsis used to use an Interface. -
C. Throwable
Explanation:Throwableis the superclass of bothErrorandException. -
C. finally
Explanation: Thefinallyblock executes always, regardless of whether an exception was thrown or caught. -
C. IOException
Explanation:IOExceptionis a Checked Exception (must be handled). The others are RuntimeExceptions (Unchecked). -
C.
void method() throws Exception
Explanation: Thethrowskeyword in the method signature declares that the method might throw an exception. -
C. Controller
Explanation: In MVC, the Controller handles user input and updates the Model. -
C. JButton
Explanation:JButtonis the Swing class for a push button. -
B. Returns a random double between 0.0 and 1.0.
Explanation:Math.random()returns adoublevalue greater than or equal to 0.0 and less than 1.0. -
D. private
Explanation:privatemembers are accessible only within the class they are defined in. -
B. Linking a method call to the method body at runtime based on the object type.
Explanation: This is the mechanism behind Method Overriding (Runtime Polymorphism).
◇Part II: UML Analysis
-
B. Private
Explanation: The minus sign (-) represents private visibility. -
A. Public
Explanation: The plus sign (+) represents public visibility. -
C. Composition
Explanation: A filled/solid diamond represents Composition (Strong relationship). -
B. They are also destroyed.
Explanation: In Composition, the lifecycle of the part is dependent on the whole. -
B. Aggregation
Explanation: An empty/hollow diamond represents Aggregation (Weak relationship). -
B. They continue to exist independently.
Explanation: In Aggregation, the parts can survive without the container. -
A. Vehicle
Explanation: The arrow points towards the parent (Superclass). -
C.
class Car extends Vehicle
Explanation: Solid line with closed arrow means Inheritance (extends). -
B. It cannot be instantiated and has abstract methods.
Explanation: Interfaces are abstract contracts. -
B.
class Guitar implements Playable
Explanation: Dashed line with closed arrow means Implementation (implements). -
C. Zero to many.
Explanation:0..*means zero or more (many). -
A. Exactly one.
Explanation:"1"means exactly one. -
C. Protected
Explanation: The#symbol stands for protected visibility. -
B. Static method
Explanation: Underlined members in UML denotestatic. -
A. Abstract class
Explanation: Italicized names denote abstract classes or methods.
◇Part III: Code Analysis
-
B.
1 2
Explanation:idis instance-specific (1, then 2).countis static (shared), so it increments to 2 and stays 2 for everyone. -
C.
false true
Explanation:s1 == s2is false (different memory addresses).s1.equals(s2)is true (same content "Java"). -
B.
B
Explanation: Polymorphism. The object is of typeB, soB's overriddenshow()method is called. -
D.
P C
Explanation: Constructor chaining. Child constructor implicitly callssuper()(Parent constructor) first. -
C.
Error Done
Explanation: Index 3 is out of bounds (0, 1, 2 exist). Exception is caught ("Error"), thenfinallyexecutes (" Done"). -
B.
[A, C, B]
Explanation:add(1, "C")inserts "C" at index 1, shifting "B" to the right. -
C. Compilation Error
Explanation:Shapeis abstract and cannot be instantiated. -
A.
IntDouble
Explanation: Method overloading selects the method matching the parameter type (intvsdouble). -
C.
Caught Next
Explanation: Exception is thrown, caught ("Caught "), and then program flow continues ("Next"). -
B. Compilation Error
Explanation: Interface variables are implicitlyfinal(constants) and cannot be reassigned. -
C.
9
Explanation: Castingdoubletointtruncates the decimal part (does not round). -
B.
false
Explanation:true && falseisfalse. -
B.
Java
Explanation: Strings are immutable.s.concat()returns a new string, butsitself remains "Java" unless reassigned. -
B.
1
Explanation:10 % 3is the remainder of 10 divided by 3 (3 * 3 = 9, remainder 1). -
A.
012
Explanation: Loop runs for i = 0, 1, 2. Stops when i = 3. -
B.
One Two
Explanation: Case 1 matches. There is nobreakafter Case 1, so it "falls through" and executes Case 2 as well. -
B.
false
Explanation: The defaultequals()implementation inObjectcompares memory addresses (like==), which are different for two new objects. -
B.
2
Explanation: Integer division (int / int) results in anint. The decimal .5 is truncated. -
A.
5 6
Explanation:x++(post-increment) prints the current value (5) then increments it. Next print shows 6. -
C.
ArrayIndexOutOfBoundsException
Explanation: Array size is 5, so valid indices are 0-4. Index 5 is out of bounds. -
B.
20
Explanation:this.x = xcorrectly assigns the parameter 20 to the instance variablex. -
A.
10
Explanation:super.xaccesses the parent's variablex(10), ignoring the child's shadowedx(20). -
B.
Null
Explanation: Calling a method onnullthrowsNullPointerException, which is caught. -
C.
3
Explanation:arr[1]selects the second array{3, 4}.[0]selects the first element of that array, which is 3. -
C. Compilation Error
Explanation:finalvariables cannot be reassigned.
◇Common Pitfalls
Checking only the letter answer without reading the explanation — understanding why is crucial for exam variants.
Memorizing code output without understanding the underlying mechanism (e.g., dynamic dispatch, String immutability).
Ignoring the UML analysis rationales — UML notation rules (visibility, line styles, multiplicity) are commonly tested.
◇Summary
TL;DR — Complete 70-item answer key with explanations covering OOP theory, UML notation rules, and code tracing mechanics (static sharing, polymorphic dispatch, constructor chaining, exception flow, autoboxing).
◇Links
Related:
Source / Reference:
Made by 2ru17
Please contact me through discord or my the university email for any problems regarding the reviewer. Thanks!
◇References & Navigation
- Related Notes: ◇Object-Oriented Programming in Java