Department of Computer Science: COMSATS University Islamabad, Abbottabad Campus
Department of Computer Science: COMSATS University Islamabad, Abbottabad Campus
Department of Computer Science: COMSATS University Islamabad, Abbottabad Campus
Question 1:
1. If an attribute of a class and local member of a method are same, how can you
differentiate?
a) By using base keyword b) By using that keyword
c) By using this keyword d) By using static keyword
3. Search out the error in the following square class constructor if any
public float square(float s) { s=0; }
a) public b) missing return keyword
c) no error d) return type
a) 100 b) 50 c) 0 d) Error
16. Which of the following is used to declare, construct, and initialize an array?
a) int arr [][] = {1, 2, 3, 4};
b) int [] arr = (1, 2, 3);
c) int [] arr = {};
d) int arr [] = {1, 2, 3};
17. What will be the output of the following Java code segment?
int[] x = new int[5];
System.out.println(x);
a) 0 b) value stored in arr[0]
c) 00000 d) Garbage value
18. Statement ob1 = ob2 cause compiler error if the objects are of different (same) classes.
TRUE FALSE
19. We can’t use access modifiers for constructors in Java? FALSE
20. What is the output of following program and show the latest value of x.
class A{
int x = 50;
A() {
x = 100;
}
void copy(int x){
x = this.x;
}
}
//main . . .
A ob = new A();
System.out.println(ob.x);
Ob.copy(30);
System.out.println(ob.x);
a) 50 b) 100 c) 30 d) Error
a) 3 b) 0 c) 6 d) 1
30. What is the return type of a method that does not return any value?
a) int b) constructor
c) void d) double
Question 2
Mr. Muhammad Umar and Mr. Muhammad Adil are looking to buy a house in a new development
town. After looking at various models, the three models they like are colonial, split-entry, and
single-story. The builder gave them the base price and the finished area in square feet of the three
models. They want to know the model(s) with the least price per square foot. Write a Java program
that accepts as input the base price and the finished area in square feet of the three models. The
program outputs the model(s) with the least price per square foot. (Make a class of your own choice
with at least three objects and methods).