Prepared by: Anas Sadek
Program: Computer Programming, Algonquin College
Date: April 2025
Table of Contents
• Pages 2–4: Long Answer Questions
• Pages 5–16: Long Answers with Detailed Explanations
• Pages 17–37: Multiple Choice Questions (MCQs)
Notes:
• Each long answer includes a detailed explanation for better understanding.
• Long answer solutions are presented on separate pages for clarity.
• MCQ answers are provided immediately after each question for ease of review.
Long Answer Practice Questions (25 Questions)
1 | Page
,1. [Custom Class Construction]
Write a Java class Book with private fields title (String) and pages (int). Include a
default constructor, an overloaded constructor, accessor and mutator methods,
and a method printDetails() that returns a formatted string containing both fields.
2. [Array Processing]
Write a method called printMax() that accepts an array of integers and prints the
maximum value. Assume the array contains at least one value.
3. [Array Processing]
Implement a method countEvenNumbers(int[] values) that prints the number of
even values in the given array.
4. [Varargs and Method Overloading]
Write a class with two overloaded methods sum(). One method takes an array of
integers as a parameter, and the other uses varargs. Both return the total sum.
5. [Memory Map Analysis]
Given the declaration Person p = new Person("Ali");, followed by p = null;, draw
and explain a memory map showing the state of the variable and heap before and
after assigning null.
6. [Encapsulation and Design Flaws]
Review the following class with public fields. Identify the design issues and
rewrite it using proper encapsulation:
public class Item {
public String name;
public int quantity;
}
7. [UML Class Diagram]
Design a UML class diagram for a Student class that includes fields for name, ID
number, and GPA. Include appropriate accessors, mutators, and a method to
display the student’s information.
8. [Method Design]
Create a method that receives two String arguments and returns a new String
that combines them with a hyphen in between (e.g., "first-second").
9. [String Equality]
Explain the difference between using == and .equals() when comparing Strings in
Java. Provide examples where each behaves differently.
10. [Loop Tracing]
Trace the following code and explain the output:
2 | Page
, int total = 0;
for (int i = 1; i <= 5; i++) {
total += i;
}
System.out.println(total);
11. [Object-Oriented Design]
Design a Car class with attributes make, model, and year. Include a constructor,
accessors, mutators, and a display() method. Then write a short main method to
create a Car object and print its data.
12. [Command Line Arguments]
Write a program that accepts two command line arguments: a first name and an
age. Display a greeting using both. Include error handling for missing or invalid
inputs.
13. [Method Behavior]
Write a method reverseArray(int[] arr) that reverses the contents of an array in-
place. Include appropriate loop logic and a test case in main.
14. [Reference Semantics]
Explain what happens when an array is passed to a method in Java. Include an
example showing that the method can modify the original array.
15. [Wrapper Classes and Autoboxing]
Describe what autoboxing and unboxing mean in Java. Provide a code example
where an Integer object is used in a context expecting an int.
16. [Formatting Output]
Write a Java method that prints a person’s name and GPA using
System.out.printf(), showing the GPA with 2 decimal places.
17. [Debugging Logic]
Given a loop that doesn’t terminate as expected, identify and fix the bug:
int i = 10;
while (i >= 0) {
System.out.println(i);
}
3 | Page
, 18. [Nested If Statements]
Write a Java method that accepts a score (0–100) and prints a grade: A for 90+, B
for 80–89, etc., using nested if/else statements.
19. [Switch Case Fall-Through]
Explain how switch-case fall-through works in Java. Write a switch example where
missing a break causes unexpected output.
20. [Scanner and Input Validation]
Write code using Scanner that asks the user to enter a number between 1 and 10.
Keep prompting until the user enters a valid number.
21. [Array of Objects]
Create an array of 3 Person objects, where Person has name and age fields.
Initialize the array, then write a loop that prints each person’s name and age.
22. [Scope of Variables]
Explain what variable scope means in Java. Give an example showing how a
variable declared in a method differs from one declared inside a loop.
23. [Static vs Instance Members]
Describe the difference between static and instance variables/methods. Provide
an example of a static counter used to count how many objects of a class were
created.
24. [Loop Filtering Logic]
Write a method that prints only the odd numbers from a given array using a for
loop and an if-statement.
25. [Design Flaws in a Class]
The following class performs printing in a getter. Identify the design issue and
rewrite it using better practices:
public class Fruit {
private String variety;
public String getVariety() {
System.out.println(variety);
return variety;
}
}
Answers and Detailed Explanations
4 | Page