Midterm Exam

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Faculty of Engineering

Programming Language III (JAVA) Computer & Control – 4 th Year


Mid-Term Exam
Dr. Sami AL -MAQTARI 05/02/2012

Q1:The following code which represents the content of the file Main.java contains some errors (or
bugs). Find the errors and explain them. Rewrite the code lines that contain the errors after
correcting them. What would be the output of the program after correction.

1) public class Main {


2) private int v = 7;
3)
4) public static void main(String[] args) {
5) Multiplier two = new Multiplier(2);
6) Multiplier three;
7) three = new Multiplier();
8) Multiplier four;
9)
10) String s1 = "--- Testing Mutltipliers ---";
11) String s2 = v + " x " + two.factor + " = " + two.calculate(v);
12) String s3 = v + " x " + three.factor + " = " + three.calculate(v);
13) String s4 = v + " x " + four.factor + " = " + four.calculate(v);
14)
15) System.out.println(s1);
16) System.out.println(s2);
17) System.out.println(s3);
18) System.out.println(s4);
19) }
20) }
21)
22) public class Multiplier {
23) public final int factor;
24)
25) public void Multiplier(int a) {
26) factor = this.a;
27) }
28)
29) public int calculate(int value) {
30) return value * factor;
31) }
32) }

Q2:What are the differences between private, protected, and public access modifiers?

Q3:Can we declare a constructor as private for a given class? If yes, how can you declare an
instance (object) of that class? If no, why not?

Page 1 of 2
Faculty of Engineering
Programming Language III (JAVA) Computer & Control – 4 th Year
Mid-Term Exam
Dr. Sami AL -MAQTARI 05/02/2012

Q4:What is the output of the following code?

1) public class Exam {


2) public static void main(String[] args) {
3) SuperClass x = new SuperClass(0);
4) SubClass y = new SubClass(0);
5) }
6) }
7) class SuperClass {
8) public SuperClass() {
9) System.out.println("Inside the SuperClass() default constructor");
10) }
11) public SuperClass(int a) {
12) System.out.println("Inside the SuperClass(int a) constructor");
13) }
14) }
15)
16) class SubClass extends SuperClass {
17) public SubClass(int a) {
18) System.out.println("Inside the SubClass(int a) constructor");
19) }
20) }

Q5:What is the principle of overloading?

Q6:Define the following:


a) A static method.
b) A static data member.
c) A final variable.
d) A constructor

Page 2 of 2

You might also like