Professional Documents
Culture Documents
ADDITIONAL MATERIALS
Not allowed.
ALLOWED TO USE:
Desks should be free from all unnecessary items (books, notes, technology, food, water, clothes)
Use of any electronic device (Phone, iPod, iPad, laptop) is not allowed during the examination.
Cheating, talking to fellow students, singing, turning back are not allowed
Write your Name (capital letters), ID number and Section number in each page of your examination paper.
Final answers must be written by only blue or black, non-erasable pen. Do not use highlighters or correction pen.
All answers should be written in the space provided for each question, unless specified the other way.
If you have a problem, please raise your hand and wait quietly for a Proctor.
You are not allowed to leave the exam room until you submit the exam papers.
STUDENT NAME:
2
STUDENT NAME:
PART A. Write answer in following table (3 pts each and no partial points):
1. 0 11. ClassA
ClassB
5. Error 15.Overloading
6. x= 1 y =1 16.overriding
3
STUDENT NAME:
5. Write the output for the following program. In case of an error write “Error” in table:
public class Mid2022 {
public static void main(String[] args) {
int a;
a = (int)5/2.0;
System.out.println(a);
}
}
4
STUDENT NAME:
6. Write the output for the following program. In case of an error write “Error” in table:
public class Mid2022 {
public static void main(String[] args) {
int x = 0;
int y = 0;
if((x++ > 1)&(++y > 1));
System.out.println("x = "+x+"y = "+y);
}
}
7. Write the output for the following program. In case of an error write “Error” in table:
public class Mid2022 {
public static void main(String[] args) {
int x = 0;
int y = 0;
if((x++ > 1)&&(++y > 1));
System.out.println("x = "+x+"y = "+y);
}
}
8. Fill blank space: In class declaration (particularly in blocks methods or constructors) the keyword
__________refers to the object itself. It can also be used inside a constructor to invoke another
constructor of the same class.
9. Fill blank space: __________are a special kind of method. They have three peculiarities:
It must have the same name as the class itself.
It does not have a return type—not even void.
They are invoked using the new operator when an object is created.
10. Fill blank space: The keyword __________ refers to the superclass and can be used to invoke the
superclass’s methods and constructors. A subclass inherits accessible data fields and methods from
its superclass.
5
STUDENT NAME:
11. Write the output for the following program. In case of an error write “Error” in table:
package mid2022;
class A{
public A(){
System.out.println("Class A");
}
}
class B extends A{
B(){
System.out.println("Class B");
}
}
public class Mid2022 {
public static void main(String[] args) {
B b = new B();
}
}
12. Write the output for the following program. In case of an error write “Error” in table:
class B{
int a = 3;
int[] b = {3,3,3};
void changeVal(int a, int[] b){
a += 7;
for(int i=0;i<3;i++) b[i] +=7;
}
}
public class Mid2022 {
public static void main(String[] args) {
int a = 1;
6
STUDENT NAME:
int[] b = {1,1,1};
B t = new B();
System.out.print(a+",");
for(int i=0;i<3;i++)
System.out.print(b[i]+",");
t.changeVal(a, b);
System.out.print(a+",");
for(int i=0;i<3;i++)
System.out.print(b[i]+",");
}
}
13. Write the output for the following program. In case of an error write “Error” in table:
abstract class A{
abstract void myMethod();
}
class B extends A{
void myMethod(){
System.out.println("Class B");
}
}
class C extends A{
void myMethod(){
System.out.println("Class C");
}
}
public class Mid2022 {
public static void main(String[] args) {
A[] a = new A[3];
a[0] = new B();
7
STUDENT NAME:
17. Fill blank space: When reference variable of Parent class refers to the object of Child class, it is
known as __________.
18. Fill blank spaces: Java’s exception-handling model is based on three operations:
____________________
____________________
____________________
PART B.
1. Write a method “factorial” for Java program (only one method) to find the Factorial of a given
number. (10 pts)
…
public int factorial(int n){
int res = 1;
for(int i=1;i<=n;i++)
res *=i;
return res;
}
OR in case of recursion
if (n==1) return n;
else return n*f(n-1);
9
STUDENT NAME:
2. Write the interface called GeometricObject, which declares two abstract methods: getParameter()
and getArea(), as specified in the class diagram. Write the implementation class Circle, with a
protected variable radius, which implements the interface GeometricObject. The class
ResizableCircle is defined as a subclass of the class Circle, which also implements an interface
called Resizable, as shown in class diagram Write a test program called TestResizableCircle to test
the methods defined in ResizableCircle. (30 pts)
Note! If you do not remember formulas for Area and Perimeter of circle, use any formulas that you
remember. Name of class “Circle” written in italic font!
Circle
interface GeometricObject{
public double getPerimeter();
public double getArea();
}
interface Resizable{
public void resize(int percent);
}
abstract class Circle implements GeometricObject{
public Circle(double radius){};
[@Override – optional]
public double getPerimeter(){};
[@Override – optional]
public double getArea(){};
}
10
STUDENT NAME:
11