NEW
Font size
WorksheetsIntroduction to OOP and Java Fundamentals
Total questions: 15
Worksheet time: 45mins
A banking application models Account as a class and uses subclasses SavingsAccount and CurrentAccount. To allow each subclass to provide its own implementation of a withdraw() method while keeping the same method signature declared in Account, which OOP concept should be applied?
Method overloading at compile time within Account
Method overriding at run time in each subclass
Encapsulating withdraw() by making it private
Using abstraction by declaring withdraw() as a static method
Given the description: "Objects have state (data), behavior (functionality), and identity (unique ID used internally by the JVM)." Which option best explains why classes are considered blueprints in OOP?
Classes store objects directly and consume memory for each instance they might create
Classes define data members and methods that specify operations on an object without storing space for the object itself
Classes only provide unique IDs to objects while behaviors are added later at runtime
Classes are logical entities that exist solely to group unrelated functions like in procedure-oriented programming
Which explanation best captures why Java is considered platform independent? Choose the option that aligns with Java’s "Write Once, Run Anywhere (WORA)" behavior as described: Java source is compiled into bytecode that runs on different JVMs (e.g., Windows, Linux, Mac/OS) without recompilation.
Java source executes directly on any operating system kernel with no intermediary layer.
Java programs are compiled into platform-specific native binaries for each target OS.
Java source is interpreted line-by-line by the operating system’s shell regardless of platform.
Java code is compiled into bytecode that the JVM for each platform executes, enabling the same class file to run across Windows, Linux, and Mac/OS.
Consider the following Java snippets: (1) class Box { double width; double height; double depth; } (2) Box mybox; (3) mybox = new Box(); Which statement best explains the role of line (2) compared to line (3)?
Line (2) declares a reference variable to a Box object, while line (3) allocates a new Box object and assigns its reference to that variable.
Line (2) creates a Box object with default values, while line (3) updates those values at runtime.
Line (2) defines the Box class's instance variables, while line (3) initializes them to zero.
Line (2) copies an existing Box object, while line (3) deletes the original and keeps the copy.
Read the Java snippet and infer the output printed for both objects. class Box { double width, height, depth; Box() { // default constructor System.out.println("Constructing Box"); width = 10; height = 10; depth = 10; } double volume() { return width * height * depth; } } class BoxDemo6 { public static void main(String[] args) { Box mybox1 = new Box(); Box mybox2 = new Box(); double vol; vol = mybox1.volume(); System.out.println("Volume is " + vol); vol = mybox2.volume(); System.out.println("Volume is " + vol); } } Which statement best explains what happens when Box objects are created without explicitly defining a constructor in the class?
The program fails to compile because Java requires a parameterized constructor for object creation.
Java provides a default constructor that initializes instance variables to their default values, allowing object creation and subsequent method calls.
Objects are created with uninitialized fields that cannot be accessed until a setter method is called.
The new operator automatically infers constructor parameters from the volume() method signature.
Given the following scenario, select the most appropriate access modifier to meet the requirement: You need a method in class A to be callable by subclasses in other packages, but it must not be accessible by non-subclass classes outside the package. Which modifier best satisfies this?
private
default (package-private)
protected
public
Select the statement that best explains the primary purpose of comments in Java code.
They are executed by the compiler to optimize performance.
They are non-executable statements used to provide information or explanations and can hide code temporarily.
They are required to declare variables and methods.
They are used only to mark TODO tasks and are ignored by programmers.
Choose the best use case for a multi-line comment based on the material.
Documenting API methods for external tools.
Commenting several consecutive lines of code within a method.
Annotating a single variable declaration.
Embedding XML metadata for the compiler.
In the example with a multi-line comment inside main, the program prints 10. Why does the presence of the comment not change the output?
Comments are executed but do not affect integer values.
Comments are ignored by the compiler and do not alter runtime behavior.
Comments convert variables to strings during printing.
Comments cause the code to be skipped at runtime.
What tool is used to generate an HTML API from documentation comments?
javac
javadoc
jar
java
Documentation comments are typically used to:
Hide code temporarily during debugging.
Produce an HTML file that documents your program’s API.
Declare constants at the top of a class.
Control compiler optimization settings.
Consider you want to temporarily disable three consecutive print statements inside main without deleting them. Based on the material, which approach is most appropriate?
Place // at the beginning of each line.
Wrap the three lines between /* and */.
Use /** and */ to tell javadoc to ignore them.
Rename the method so the lines won’t execute.
Which statement about where comments can appear is most aligned with the material?
Comments can be placed to provide information about variables, methods, classes, or any statement.
Comments must only appear above class declarations.
Comments can only appear at the end of a file.
Comments cannot be used inside methods.
You are preparing library code for others to use. According to the material, how should you annotate methods to enable automatic API documentation generation?
Use // above each method and run javac.
Use /* ... */ around the entire class and run java.
Use /** ... */ above each method and run javadoc.
Use /// above each method and run jar.
Consider the Java snippet: long lightspeed = 186000; int days = 1000; long seconds = days * 24 * 60 * 60; long distance = lightspeed * seconds; Which statement best explains why long was used for distance rather than int, based on Java data types and variable behavior described?
int can store larger whole numbers than long, so long is unnecessary here.
distance requires fractional precision, which only long provides among primitives.
The computed value exceeds the maximum range of int, so a wider whole-number type like long is required.
long variables are created and destroyed with objects, while int variables are not.
