Java Final Exam 2015-2016 With Solution
Java Final Exam 2015-2016 With Solution
Q1. What are final variables, final methods, and final classes in Java?
Final variable: it’s a variable that you can’t change its value after it has been initialized. It
keeps its initial value until the end of its lifetime.
Final methods: are methods in the superclass that you declare as final, so they can’t be
overridden in any subclass.
Final class: a final class is a class that you can’t inherit from it. So it can’t have any subclasses at all. Also
known as the last leave in the inheritance hierarchy.
Q2. Explain briefly what is the difference between a reference and an object.
A reference is a variable that points ( or holds a pointer) to point to where an object is
stored in memory . it contains the address of that object in the memory. It can be thought as a
handle that you use to tie something or attach it.
An object: is an actual data, an instance of a class that is stored in a memory slot. It is
created using the word new, it has real world data entity.
Q3. Explain briefly how can you prevent a class from been inherited.
A class is prevented from being inherited using the word final, we declare it as a final class.
Q4. Does the following program compile correctly? If yes, what will be the output of the program?
If no, what is/are the error(s)?
public class Test
{
private static int[] x;
public static void main(String[] args)
{
System.out.println(x[0]);
}
}
NO: it causes runtime error, because the element in x[0] has NOT been initialized.
1/2
Programming Languages III – Java Electrical Engineering Dept.
Control & Computer Section
Final Exam 2015/2016 – 4th year - 2nd Term
Q5. The file “Testing.java” contains the following code with some errors. Find and explain these
errors and explain how to correct them?
Delete it. The only public class allowed should have the same name as the file which is class Test
2- must add the word abstract Because there is one abstract method in this class.
// Testing.java
public class A {
Not Error. But maybe logical. This variable isn’t used, not necessary
public A(int x){}
public abstract void fn();
}
1- Must override abstract method.
class B extends A { } 2- Must declare constructor that passes a value to super
public class constructor
class Test {
public static void main (String args [])
{
A a = new B();
System.out.println("complete");
}
}
Correct Code:
package test;
abstract class A {
public A(int x) {
}
public abstract void fn();
}
class B extends A {
public B(){
super(3); // superclass constructor must get a value
}
public void fn(){
System.out.println("just to complete the abstract method.");}
}
public class Test {
public static void main(String args[]) {
A a = new B();
System.out.println("complete");
}
}
2/2
Programming Languages III – Java Electrical Engineering Dept.
Control & Computer Section
Final Exam 2015/2016 – 4th year - 2nd Term
Q6. What is the output of the following program? Explain your answer.
class Tree { }
class Pine extends Tree { }
class Oak extends Tree { }
Output:
Pine
Tree
The reason is this: the reference if of type Tree tree refers to an object of type Pine.
Where any object of type is both a Pine and a Tree.
3/2
Programming Languages III – Java Electrical Engineering Dept.
Control & Computer Section
Final Exam 2015/2016 – 4th year - 2nd Term
Q7. What is the output of the following program? Explain your answer.
public class Main extends SuperMain {
static{ System.out.println("Main static");}
{ System.out.println("Main non-static static");}
public Main() {
}
System.out.println("Main
constructor"); Important
public static void main(String[] args)
{ System.out.println("main()
begins"); Main x = new Main();
Main y = new Main();
System.out.println("main()
ends");
}
}
class SuperMain {
static{ System.out.println("Super static");}
{ System.out.println("Super non-static");}
public SuperMain() {
System.out.println("Super
constructor");
}
}
لذالك سنقوم بحذف األمر.سنقوم بنتفيذ الكود و ذالك بوجود أمر انشاء اوبجكت واحدة فقط
4/2
Programming Languages III – Java Electrical Engineering Dept.
Control & Computer Section
Final Exam 2015/2016 – 4th year - 2nd Term
-٢يتم تنفيذ ال initializing blockو بعده ال constructorالي هما في نفس الكلس و كان ال initializing blockيعتبر أول جمله
توضع داخل ال constructor .يتم تنفيذ هوالء عند انشاء اوبجكت فقط باستخدام ال new.و يتم تنفيذ هم بنفس التسلسل الي فوق .يعني
يبدأ بال Superclassاعلى كلس في سلسله الوراثه ينفذ حقه ال initializing blockو constructorمن ثم ينزل الكلس الي بعده حتى
يصل الكلس الي تم انشاء االوبجكت منه .يعني تنازليا من أعلى شجره الوراثه الي اسفلها.
هوال القاعدتين التي يعرفان إليه تنفيذ ال initializing blockو ال static initializing blockو ال constructor.
أي اوامر ثانيه خارج هذا اإلطار تنفذ عند مرور ال scopeمن عندها5 / 2.حاول تطبيق القواعد في الكود الي فوق ثم في السؤال.
Programming Languages III – Java Electrical Engineering Dept.
Control & Computer Section
Final Exam 2015/2016 – 4th year - 2nd Term
هذا هو حل السؤال ( حاول متابعه تنفيذ الكود بورقه قبل:حل السوال هو االتي في وجود االمر الذي تم حذفه
:رؤيته) حاول للكودين
6/2
Programming Languages III – Java Electrical Engineering Dept.
Control & Computer Section
Final Exam 2015/2016 – 4th year - 2nd Term
Q8. What is the output of the following program? Show how you can change the class Box into a
generic class in order to avoid the need for casting the return value of get() method and the
bugs/errors shown in the last comments.
public class Box {
private Object t;
integerBox.add(new Integer(10));
stringBox.add("Hello World");
Output:
Integer Value : 10
String Value : Hello World
7/2
Programming Languages III – Java Electrical Engineering Dept.
Control & Computer Section
Final Exam 2015/2016 – 4th year - 2nd Term
Good luck.
8/2