You are on page 1of 11

STUDENT NAME:

STUDENT ID NUMBER: SECTION NUMBER IN THIS COURSE:

MID-TERM EXAMINATION FALL 2022

COURSE NAME: Application Programming in Java

COURSE NUMBER: SOC2030

EXAMINATION DATE: TIME:

EXAMINATION DURATION: 60 minutes

ADDITIONAL MATERIALS
Not allowed.
ALLOWED TO USE:

SPECIAL INSTRUCTIONS: All questions compulsory.

Please do not open the examination paper until directed to do so.


___________________________________________________________________
READ INSTRUCTIONS FIRST:

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.

You should choose one answer in the multiple-choice question.

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:

STUDENT ID NUMBER: SECTION NUMBER IN THIS COURSE:

2
STUDENT NAME:

STUDENT ID NUMBER: SECTION NUMBER IN THIS COURSE:

PART A. Write answer in following table (3 pts each and no partial points):

1. 0 11. ClassA
ClassB

2. null 12. 1,1,1,1,1,8,8,8,

3. 0.0 13. Class B


Class B
Class C
4. false 14. Error

5. Error 15.Overloading

6. x= 1 y =1 16.overriding

7.x=1 y=0 17. Upcasting

8. this 18. declaring an exception


throwing an exception
catching an exception
9.constructor 19. true

10. super 20. false

3
STUDENT NAME:

STUDENT ID NUMBER: SECTION NUMBER IN THIS COURSE:

Consider following program:


package mid2022;
class TestClass{
int a;
String b;
float c;
boolean d;
}
public class Mid2022 {
public static void main(String[] args) {
TestClass t = new TestClass();
System.out.println(t.a); //1
System.out.println(t.b); //2
System.out.println(t.c); //3
System.out.println(t.d); //4
}
1. What is the output will be for statement with comment “//1” after execution of this program? In case
of an error write “Error” in table.
2. What is the output will be for statement with comment “//2” after execution of this program? In case
of an error write “Error” in table.
3. What is the output will be for statement with comment “//3” after execution of this program? In case
of an error write “Error” in table.
4. What is the output will be for statement with comment “//4” after execution of this program? In case
of an error write “Error” in table.

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:

STUDENT ID NUMBER: SECTION NUMBER IN THIS COURSE:

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:

STUDENT ID NUMBER: SECTION NUMBER IN THIS COURSE:

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:

STUDENT ID NUMBER: SECTION NUMBER IN THIS COURSE:

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:

STUDENT ID NUMBER: SECTION NUMBER IN THIS COURSE:

a[1] = new B();


a[2] = new C();
for(int i=0;i<3;i++)
a[i].myMethod();
}}
14. 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(String str){
System.out.println(str);
}
}
class C extends A{
void myMethod(String str){
System.out.println(str);
}
}
public class Mid2022 {
public static void main(String[] args) {
A[] a = new A[3];
a[0] = new B();
a[1] = new B();
a[2] = new C();
for(int i=0;i<3;i++)
a[i].myMethod("Test class");
}}
15. Fill blank space: __________ means to define multiple methods with the same name but different
signatures.
16. Fill blank space: __________ means to provide a new implementation for a method in the subclass.
8
STUDENT NAME:

STUDENT ID NUMBER: SECTION NUMBER IN THIS COURSE:

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:
 ____________________
 ____________________
 ____________________

Consider following program:


public class Mid2022 {
public static void main(String[] args) {
String s1 = "Hello Java";
String s2 = "Hello Java";
String s3 = new String("Hello Java");
System.out.println(s1 == s2); // 1
System.out.println(s1 == s3); // 2
}}
19. What is the output will be for statement with comment “//1” after execution of this program? In case
of an error write “Error” in table.
20. What is the output will be for statement with comment “//2” after execution of this program? In case
of an error write “Error” in table.

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:

STUDENT ID NUMBER: SECTION NUMBER IN THIS COURSE:

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:

STUDENT ID NUMBER: SECTION NUMBER IN THIS COURSE:

class ResizableCircle extends Circle iplements Resizable{


public ResizableCircle(double radius){};
[@Override – optional]
public void resize(int percent){};
}

In red color are most important elements.

11

You might also like