MODEL QUESTION PAPER
APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY
THIRD SEMESTER B. TECH DEGREE EXAMINATION, MONTH AND YEAR
Course Code: PBCST304
Course Name: Object Oriented Programming
Max. Marks: 40 Duration: 2 hours 30 minutes
PART A
Answer all questions. Each question carries 2 marks CO Marks
1 You compile MyApp.java on Windows into MyApp.class, then copy that CO1 (2)
same MyApp.class file to both a Linux machine and a macOS system.
Without recompiling, you run:
java MyApp
on each machine, and it executes successfully.
What is the name of the compiled file format that makes this possible?
2 What will be the value of a after the execution of the following code CO1 (2)
segment in Java.
int a = -1;
a = a >> 4;
3 You’re given this Java code: CO2 (2)
class Vehicle {
protected String type = "Generic Vehicle";
Vehicle() {
System.out.println("Vehicle created");
}
}
class Car extends Vehicle {
protected String type = "Car";
Car() {
// **INSERT STATEMENT A HERE**
System.out.println("Type: " + type);
}
void printParentType() {
System.out.println("Parent Type: " +
Page 1 of 5
super.type);
}
}
public class TestSuper {
public static void main(String[] args) {
Car c = new Car();
c.printParentType();
}
}
a. What exact statement must replace // **INSERT STATEMENT A
HERE** so that when you run TestSuper, you see:
Vehicle created
Type: Car
Parent Type: Generic Vehicle
b. In one sentence each, explain:
(i) What the statement you inserted does here.
(ii) Why super.type prints "Generic Vehicle" while type prints "Car".
4 Illustrate inheritance in Java. CO2 (2)
5 Using proper examples, give the difference between interfaces and abstract CO3 (2)
classes in Java.
6 You’re given this Java class: CO2 (2)
1 import java.io.*;
2 public class ExceptionDemo {
3 public static int divide(int a, int b) {
4 return a / b;
5 }
6 public static void readFile(String path) throws
IOException {
7 FileInputStream fis = new FileInputStream(path);
8 fis.close();
9 }
10 public static void main(String[] args) {
11 String filePath = args[0];
12 int x = Integer.parseInt(args[1]);
13 int y = Integer.parseInt(args[2]);
Page 2 of 5
14 try {
15 readFile(filePath);
16 System.out.println("Result: " + divide(x,
y));
17 } catch (Exception e) {
18 e.printStackTrace();
19 }
20 }
21 }
(i) At which lines in this code might a checked exception be thrown?
(ii) At which lines might an unchecked exception occur?
(iii) For each case, name the exception class and explain briefly why it’s
checked or unchecked.
7 Give the differences between AWT and Swing in Java. CO5 (2)
8 List the main steps to establish a JDBC connection. CO5 (2)
What is a PreparedStatement, and why is it preferred over a Statement?
PART B
Answer any one full question from each module. Each question carries 6 marks
Module 1
9 a) Write a Java program that uses command line arguments to calculate area: CO1 (4)
If the user enters one argument, the program should treat it as the side
of a square and print the area of the square.
If the user enters two arguments, the program should treat them as the
length and breadth of a rectangle and print the area of the rectangle.
If the number of arguments is not 1 or 2, display an appropriate error
message.
b) Illustrate the use of this keyword in Java. CO2 (2)
10 a) Design a Java class Book with the following attributes: title, author, and CO2 (4)
price.
Write a parameterized constructor to initialize these values.
Also write a default constructor that sets default values.
Create a display() method to print the book details.
In the main method, create two objects — one using the default
constructor and another using the parameterized constructor — and
display their details.
Page 3 of 5
b) Refactor the Book class so that (i) data validation and (ii) input/output each CO1 (2)
live in their own classes. Which SOLID principle are you applying, and
why?
Module 2
11 a) Create a Java class Calculator that demonstrates method overloading by CO2 (3)
implementing the add() method in the following ways:
add(int a, int b) — returns the sum of two integers.
add(double a, double b) — returns the sum of two double values.
add(int a, int b, int c) — returns the sum of three integers.
In the main method, create an object of Calculator and call all the overloaded
add() methods with appropriate arguments. Display the results.
b) Create a Java class Counter that has a static variable count to keep track of CO2 (3)
the number of objects created. Increment count in the constructor. In the
main method, create three objects of the Counter class and display the total
number of objects created using the static variable.
12 Define a Java base class Animal with a method makeSound() that prints a CO2 (3)
generic sound. Then, create two subclasses Dog and Cat that override the
makeSound() method to print "Bark" and "Meow" respectively. In the main
method, create objects of Dog and Cat and call the makeSound() method to
show runtime polymorphism.
Module 3
13 You have a third-party class AnalyticsService with method CO4 (6)
sendEvent(String). Write an Adapter so that it conforms to your Logger
interface (log(String)). Show both class definitions and the adapter code.
14 Implement the Singleton pattern for a Logger class that writes log entries to a CO2 (6)
file. Show your code and explain how it ensures a single instance.
Module 4
15 Design a simple Java Swing GUI application with the following features: CO5 (6)
A window with two buttons labeled “Greet” and “Clear”.
A text field where the user can enter their name.
When the “Greet” button is clicked, display “Hello, [Name]!” in a
label.
When the “Clear” button is clicked, clear the text field and the label.
Use appropriate event handling by implementing ActionListener and
demonstrate how to register and handle events in Java Swing.
In your Swing GUI, the user enters a name and clicks ‘Save’. Write the
Page 4 of 5
JDBC code to insert that name into table greetings(name VARCHAR),
handling exceptions appropriately.
Write the complete code for this application.
16 a) Draw an MVC diagram for this Swing+JDBC app. CO5 (3)
b) Describe how you’d apply Dependency Inversion so the Controller can be CO5 (3)
unit-tested without a real database.
*****
Page 5 of 5