0% found this document useful (0 votes)
25 views8 pages

Java Final Exam 2015-2016 With Solution

The Box class stores objects of any type. This leads to issues like needing to cast the returned value from the get() method, and risks errors if the wrong type is cast. [1] The output shows the integer and string values stored and retrieved from two Box objects. [2] The Box class can be made generic to specify the type of object stored, avoiding casts and potential errors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views8 pages

Java Final Exam 2015-2016 With Solution

The Box class stores objects of any type. This leads to issues like needing to cast the returned value from the get() method, and risks errors if the wrong type is cast. [1] The output shows the integer and string values stored and retrieved from two Box objects. [2] The Box class can be made generic to specify the type of object stored, avoiding casts and potential errors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Programming Languages III – Java Electrical Engineering Dept.

Control & Computer Section


Final Exam 2015/2016 – 4th year - 2nd Term

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 { }

public class Forest {


public static void main(String[] args) {
Tree tree = new Pine();
if (tree instanceof Pine)
System.out.println("Pine");
if (tree instanceof Tree)
System.out.println("Tree");
if (tree instanceof Oak)
System.out.println("Oak");
}
}

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");
}
}

‫ لذالك سنقوم بحذف األمر‬.‫سنقوم بنتفيذ الكود و ذالك بوجود أمر انشاء اوبجكت واحدة فقط‬

Main y= new Main();


.‫و سيكون الكود كما في الصفحه التالية بعد حذف هذا األمر‬

4/2
‫‪Programming Languages III – Java‬‬ ‫‪Electrical Engineering Dept.‬‬
‫‪Control & Computer Section‬‬
‫‪Final Exam‬‬ ‫‪2015/2016 – 4th year - 2nd Term‬‬

‫يتم التنفيذ االوامر بحسب الترتيب التالي‪:‬‬


‫‪ -1‬أوال يتم تنفيذ ال ‪static initializing block‬و ذالك الول مرة يتم فيها استدعاء ال ‪class‬أما باستدعاء داله(دوال من نوع ستاتك)‬
‫فيه أو بانشاء اوبجكت منه‪ .‬و يتم تنفيذه مره واحدة فقط و هي أول مرة يتم فيها استدعاء الكلس‪ .‬إذا وجد حاله وراثه فان هذا البلوك هو‬
‫أول ما بتنفذ و يبدأ التنفيذ للبلوك حق األب الي في اعلى سلسله الوراثه نزوال حتى الكلس الي تم استدعائة بهذا الترتيب‪.‬‬

‫‪ -٢‬يتم تنفيذ ال ‪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;

public void add(Object t) { this.t = t; }


public Object get() { return t; }

public static void main(String[] args) {


Box integerBox = new Box();
Box stringBox = new Box();

integerBox.add(new Integer(10));
stringBox.add("Hello World");

Integer i = (Integer) integerBox.get(); // casting is needed here


String s = (String) stringBox.get(); // casting is needed here

System.out.println("Integer Value : " + i);


System.out.println("String Value : " + s);

// integerBox.add("any string"); // logical bug


// Integer x = (Integer) integerBox.get(); // casting error
}
}

Output:
Integer Value : 10
String Value : Hello World

New code with generic:


Add the generic to the class and change every object to T:

7/2
Programming Languages III – Java Electrical Engineering Dept.
Control & Computer Section
Final Exam 2015/2016 – 4th year - 2nd Term

Good luck.

8/2

You might also like