[go: up one dir, main page]

100% found this document useful (1 vote)
148 views6 pages

Department of Computer Science: COMSATS University Islamabad, Abbottabad Campus

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

COMSATS University Islamabad, Abbottabad Campus

Department of Computer Science

SESSIONAL – I EXAMINATION – FALL 2020


Class: BSE 3B Date: 22nd OCT 2020
Subject: Object Oriented Programming Max Marks: 40
Time Allowed: 1:15 min.

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

2. Why is a constructor useful?


a) Because it gives us a way of allocating memory dynamically
b) Because it ensures that object stay in their original scope
c) Because is hides the details of object creation from the user
d) Because it ensures that objects are properly initialized

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

4. What is byte code in the context of Java?


a) The type of code generated by a Java Virtual Machine
b) The type of code generated by a Java compiler
c) It is another name for a Java source file
d) It is the code written within the instance methods of a class.

5. What is the stored in the object ob in following lines of code?


class Box{
. . . . . }
Box ob;
a) null b) memory address of allocated memory of object.
c) Any arbitrary pointer d) Garbage

6. Which of the following may be part of a class definition?


a) instance variables b) instance methods
c) constructors d) all of above

7. What is garbage collection in the context of Java?


a) When all references to an object are gone, the memory used by the object is
automatically reclaimed
b) The operating system periodically deletes all of the java files available on the system.
c) Any package imported in a program and not used is automatically deleted.
d) JVM checks output of any Java program and deletes anything that doesn't make sense.

8. Which of the following is TRUE?


a) In java, an instance field declared public generates a compilation error.
b) int is the name of a class available in the package java.lang
c) Instance variable names may only contain letters and digits.
d) A class has always a constructor (possibly automatically supplied by the java
compiler).

9. What is the output of following program segment?


class Test {
int x = 50;
Test() {
x = 100;
}
}
//main . . .
Test ob = new Test();
System.out.println(ob.x);

a) 100 b) 50 c) 0 d) Error

10. What is the output of this program segment?


class box {
int width, height, length;
}
//main. . .
box obj = new box();
System.out.println(obj);
a) 0 b) 1
c) Garbage value d) Runtime error

11. In JAVA arrays are ___________


a) Objects b) Object reference
c) Primitive Data type d) Group data type
12. In the following array declaration
int[] x = new int[100];
x[80] = x[37] + x[21];
Updates the contents of the 79th component of the array x.
False

13. What will be output of the following code?


class A{
public static void main(String[] aa){
int[]x = new int[5];
System.out.println(“x[3] is “+x[3]);
}}
a) Compilation error, because size of array is not specified when declaration
b) Run time error because array elements are not initialized.
c) Display only 0
d) Run time error because x[3] is not defined
14. Which of the following statements is correct?
a) Public method is accessible to all other classes in the hierarchy
b) Public method is accessible only to subclasses of its parent class
c) Public method can only be called by object of its class.
d) Public method can be accessed by calling object of the private class.

15. Which of following is a valid statement?


a) char [] ch = new char();
b) char [] ch = new char(30);
c) char [] ch = new char[];
d) char [] ch = new char[30];

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

21. What will be the output of the following program segment?


//main. . .
int j = 1;
while (j <= 15) {
String k = j % 2 == 1 ? "***" : "+++++";
System.out.println(k);
j++;
}
a) 15 times *** b) 15 times +++++
c) 8 times *** and 7 times +++++ d) Both will print only once

22. What is the output of this program?


class A {
public static void main(String args[])
{
int i = 1;
while(i++<=100);
System.out.println(i);
}
}
a) 101 b) 1 c) 102 d) Error

23. The most significant role of methods in Java is to______________________


a) Give a name to a block b) Maximize program size
c) Help to organize a program into conceptual modules
d) Accept anything
24. A byte code is generated by JVM.
FALSE
25. What is the output of this program?
class Demo{
public static void main(String args[]){
Int[]x = {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 6;
n = x[x[n]/2];
System.out.println(x[n]/2);
} }

a) 3 b) 0 c) 6 d) 1

26. Which statement is not true in java language?


a) A public member of a class can be accessed in all the packages.
b) A private member of a class cannot be accessed by the methods of the same class.
c) A private member of a class cannot be accessed from its derived class.
d) A public member of a class can be accessed from its derived class.

27. The expression !(x>0) is true only if x has negative value.


TRUE

28. What would be behavior if the constructor has a return type?


a) Compilation error b) Runtime error
c) Compilation and runs successfully d) Only String return type is allowed

29. What is false about constructor?


a) Constructors cannot be synchronized in Java
b) Java does not provide default copy constructor
c) Constructor can have a return type
d) “this” can be used in a constructor

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).

You might also like