Solutions Exam Questions Set 1 CSC2106 Object-Oriented Design & Programming
Solutions Exam Questions Set 1 CSC2106 Object-Oriented Design & Programming
Marking guide
Section - A (10 pts)
(Very Short Answer Questions)
1) (i) How does JAVA achieve platform independence?
(ii) What are the commands used to compile and run the JAVA Programs.
(iii) Determine the value of each of the following logical expressions if a = 5, b = 10 and c =
–6
a) a>b && a<c
b) a<b && a>c
c) a==c|| b>a
d) b>15 && c<0 || a>0
(iv) Explain any three string functions used in JAVA.
(v) Explain the difference between do-while statement and while statement.
(vi) Define an abstract class.
(vii) What is the Java virtual machine (JVM)?
Answers:
1) (i) - Platform independence is achieved in JAVA through bytecode compilation and the
use of the Java Virtual Machine (JVM).
(vii) - An abstract class is a class that cannot be instantiated and may contain abstract
methods.
(x) - JVM is the virtual machine that executes Java bytecode and provides platform
independence.
Section B: (30 pts)
1. (5 pts) What are the values of the following variables?
(a) double var1 = 10 / 3;
(b) int var2 = (int) (2.5 * 2.6);
(c) boolean var3 = !(3 > 3);
(d) boolean var4 = (121 % 11 == 0) || (5 == 5);
(e)int var5 = 11 % 3;
Answer
(a) 3.0 double var1 = 10 / 3;
(b) 6 int var2 = (int) (2.5 * 2.6);
(c) true boolean var3 = !(3 > 3);
(d) true boolean var4 = (121 % 11 == 0) || (5 == 5);
(e) 2 int var5 = 11 % 3;
2. (3 points) Declare a variable to hold the amount of money in your bank account and
initialize it to
245.25. Name your Java variable in a meaningful way so that any programmer would know
what
value it holds. Your variable name should be at least two words.
answer
(a) str.length()
return type: int , value: 12
(b) str.equalsIgnoreCase("HOW ARE YOU")
return type: boolean , value: false
(c) str.indexOf("ou")
return type: int , value: 9
(d) str.lastIndexOf(" ")
return type: int , value: 7
(e) str.charAt(6)
return type: char , value: ‘e’
(f) str.substring(1, 6)
return type: String , value: “ow ar”
5. (3 points) What are uses of final keyword with variable, method and class?
Give an example for each.
Answer:
- Final class can’t be inherited. (1)
Ex. final class person{}
If we write this: class studen extends person {} give an error.
- Final variable mean it is constant and can’t be changed. (1)
Ex. final int x=5;
- Final method can’t be overriden. (1)
Ex. class A{ final void method1(){}
}
classs B extends A{
void method1() {} make an error can’t override final method
}
6. (1 pts) What the difference between usage of throws and throw keyword?
Answer:
Main difference between throw and throws is in there usage and
functionality.
- throws is used in method signature to declare Exception possibly
thrown by any method. (0.5)
- throw is actually used to throw an Exception in Java code.
(0.5)
7.(2 pts) Identify the correct output for the following program fragments. Give the reason.
i.
try
{
int x = 0;
int y = 5 / x;
}
catch (Exception e)
{
System.out.println("Exception");
}
catch (ArithmeticException ee)
{
System.out.println(" Arithmetic Exception");
}
System.out.println("Finished");
a) Exception
b) Arithmetic Exception
c) Compilation fails
d) Finished
Answer:
Compilation fails because ArithmeticException has already been
caught.ArithmeticException is a subclass
of java.lang.Exception, by time theArithmeticException has
been specified it has already been caught by theException class.
ii.
public class FinalBlock {
public static void main(String[] a){
try{
int x = 16/2;
}
catch(Exception ex){
System.out.println("A");
}
finally {
System.out.println("B");
}
try{
int x = 16/0;
}
catch(Exception ex){
System.out.println("C"); }
finally {
System.out.println("D"); }
}
}
Answer:
a) ABCD
b) ABC
c) ACD
d) BCD
8. ( 2 pts) Marks Obtained:
Identify and explain what are OOP Principles used in the following program.
abstract public class Animal {
abstract public void MakeVoice();
}
public class Cat extends Animal {
int LegsNumbers;
public void MakeVoice() {
System.out.println("Meow!");
}
}
public class Dog extends Animal {
String Color;
public void MakeVoice() {
System.out.println("Woof!");
}
9) Inheritance (3 pts)
Consider the following code.
class A {
private int x;
public A(int x) {
this.x= x;
}
public void m() {
System.out.println(x-1);
}
}
class B extends A{
private int y;
public B(int y) {
super(y-1);
this.y= y;
}
public void m() {
System.out.println(y+1);
}
}
(i) 1 point What is printed to the console by (new A(3)).m()?
2
(ii) 1 point What is printed to the console by (new B(3)).m()?
4
(iii) 1 point What is printed to the console by ((A)(new B(3))).m()?
4
10. (6 pts )Write a Swing application called SwingAdder as shown. The "ADD" button adds
the two integers and display the result. The "CLEAR" button shall clear all the text fields.
Hints: Set the content-pane to 4x2 GridLayout. The components are added from left-to-right,
top-to-bottom.
import java.awt.*; // Using AWT's layouts
import java.awt.event.*; // Using AWT's event classes and listener interfaces
import javax.swing.*; // Using Swing's components and container
The library required to connect to MySQL database using Java programming is called
"JDBC" (Java Database Connectivity). To include it in your project, you need to follow
these steps:
- Download the JDBC driver for MySQL from the official MySQL website or a
trusted source.
- Add the JDBC driver JAR file to your project's classpath. This can be done by
either copying the JAR file to your project's directory and adding it to the build path in
your IDE, or by specifying the JAR file location in the classpath configuration of your
project.
2. DescribeDescribe the parameters of the following function:
DriverManager.getConnection("jdbc:mysql://Mocalhost:3306/ict", "root", "root")
- `jdbc:mysql://localhost:3306/ict`: This specifies the URL of the MySQL database
server. Here, "localhost" represents the hostname or IP address of the server, "3306" is
the port number, and "ict" is the name of the database.
- `"root"`: This is the username used to connect to the MySQL database.
- `"root"`: This is the password associated with the username.
4. From the above source code what is the name of the database and the table it contains?
From the given source code, the name of the database is "ict" and the table it contains
is "employee".
5. What is the meaning of the following code? public class Authentification extends
JFrame implements ActionListener {
The code `public class Authentification extends JFrame implements ActionListener`
defines a Java class named "Authentification". It extends the `JFrame` class, which
means it inherits the functionality of a JFrame window. The class also implements the
`ActionListener` interface, indicating that it can handle events triggered by user
actions.
public LoginForm() {
lblUsername = new JLabel("Username:");
lblPassword = new JLabel("Password:");
txtUsername = new JTextField();
txtPassword = new JPasswordField();
btnLogin = new JButton("Login");
setTitle("Login Form");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
(a) Class Car is an abstract class. Explain the role of an abstract class. [1 marks]
(b) Write a Java version of class Car assuming it has this constructor:
public Car(double price, int year)
and that the method calculateSalePrice ( ) is abstract. [3 marks]
(c) Write a Java version of class ClassicCar assuming it has this constructor:
public ClassicCar (double price, int year) [2 marks]
and that the method calculateSalePrice ( ) returns 10,000 as the sale price of the car.
d) Write a Java version of class SportCar assuming it has this constructor:
public SportCar(double price, int year) [3 marks]
and that the method calculateSalePrice ( ) calculates the sale price of the car as follow:
if year > 2000 then the sale price is 0.75 * its original price; if year > 1995 then the sale
price
is 0.5 * its original price; otherwise the sale price is 0.25 * its original price
e) Write a Java version of class CarExhibition assuming it has this constructor:
public CarExhibition( ) [4 marks]
where CarExhibition has cars of different types stored in an arraylist and getTotalPrice
method that
returns the total prices of all cars in the exhibition.
0.5 mark
0.5 mark
2 mark
(e) CarExhibition Class Definition
import java.util.ArrayList;
import java.util.Iterator;
public class CarExhibition
{ private ArrayList cars;
public CarExhibition()
{ cars = new ArrayList(); }