OBJECT ORIENTED PROGRAMMING - FINAL EXAM
College of Information Technology
Student Name: ________________________
Student ID: ________________________
Date: ___/___/20__
Question 1: Multiple Choice (6 points)
Select the correct answer based on your knowledge of Java Constructors.
1. What distinguishes a constructor from a regular Java method?
  1. Constructors must have the `void` return type.
  2. Constructors do not have a return type, not even void.
  3. Constructors can have any name, unlike methods.
  4. Constructors must be called manually after an object is created.
Answer: B. Constructors do not have a return type-not even void. [cite: 313, 399]
2. When does Java automatically provide a default constructor for a class?
  1. Java always provides a default constructor, regardless of other code.
  2. Only if the class does not contain any variables.
  3. Only if no constructors are explicitly defined in the class.
  4. Only when you use the `new` keyword without arguments.
Answer: C. A default constructor is provided automatically only if no constructors are explicitly defined in the class. [cite: 326]
3. Which of the following best defines "Constructor Overloading"?
  1. Creating a constructor that overrides the default memory limits.
  2. Having multiple constructors in one class, each with different parameters.
  3. When a constructor calls another method inside the same class.
  4. Using a constructor to return multiple data types.
Answer: B. Constructor overloading is having multiple constructors in one class - each with different parameters (number, type, or order of parameters). [cite: 372]
Question 2: Code Tracing (8 points)
Read the following code snippets carefully. Determine exactly what will be printed to the console, or state if there is an error.
1. What is the output of the following code?
class Server { String ipAddress; Server(String ip) { ipAddress = ip; } } public class Main { public static void main(String[] args) { Server s1 = new Server(); System.out.println(s1.ipAddress); } }
Output: ERROR (Compile-time error). Explanation: The programmer defined a parameterized constructor `Server(String ip)`. Therefore, Java stops adding the default empty constructor automatically. Calling `new Server()` with no parameters causes an error because no matching constructor exists. [cite: 354, 355, 360, 365]
2. What is the output of the following code?
class Student { String name; int age; Student() { name = "Unknown"; age = 18; } Student(String n, int a) { name = n; age = a; } } public class Main { public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student("Ahmad", 22); System.out.println(s1.name + " is " + s1.age); System.out.println(s2.name + " is " + s2.age); } }
Output: Unknown is 18 Ahmad is 22 Explanation: `s1` uses the default constructor which sets the values to "Unknown" and 18. `s2` uses the overloaded parameterized constructor, which sets the values to "Ahmad" and 22. [cite: 381, 382, 383, 392, 393, 394, 395, 397]
Question 3: Programming Tasks (10 points)
Write complete Java code to solve the following standard programming problems.
1. The Network Profile System:
Write a complete Java program that defines a class named NetworkProfile. Follow these requirements carefully:
class NetworkProfile { String username; int accessLevel; // Default constructor NetworkProfile() { username = "Guest"; accessLevel = 1; } // Parameterized constructor NetworkProfile(String u, int a) { username = u; accessLevel = a; } } public class Main { public static void main(String[] args) { // Uses default constructor NetworkProfile user1 = new NetworkProfile(); // Uses parameterized constructor NetworkProfile user2 = new NetworkProfile("Admin", 5); System.out.println("User 1: " + user1.username + ", Level: " + user1.accessLevel); System.out.println("User 2: " + user2.username + ", Level: " + user2.accessLevel); } } [cite: 381, 382, 383, 392, 393, 394, 395, 397]