Oopc I Paper 3
Oopc I Paper 3
1. Select the correct answer to the following question from the answer list specified below.
class A {
A a = new A();
System.out.println(a.x); //Line 1
System.out.println(A.x); //Line 2
}
}
2. Choose the correct answer from the list of answers for the
following question.
class Dog {
class A {
int x = 10;
int y = 20;
int z = 30;
A a = new A();
a.x = a.y;
System.out.println(a.x);
System.out.println(a.y);
System.out.println(a.z);
}
}
a. Compile Error
b. 10 , 20 , 30
c. 10 , 10 , 30
d. 10 , 20 , 20
e. 20 , 20 , 30
3. Select the correct answer to the following question from the answer list specified below.
class A {
a. 290
b. 195195
c. 195ab
d. ab195
e. abab
4. Study the question and select the correct answer(s) from the answer list given below.
interface A {
5. Select the correct answer to the following question from the answer list specified below.
class Gun {
BulletCount b;
class BulletCount {
int count;
}
a. 50
b. Fire
c. 50 fire
d. Fire 50
e. Compile Error
6. Select the correct answer to the following question from the answer list specified below.
class A {
a. 10 , 20
b. Compile Error at Line 1
c. Compile Error at Line 2
d. Runtime Exception
e. None of the above
7. Choose the correct answer from the list of answers for the following question.
class A {
System.out.println(x[1]);
System.out.println(x);
}
}
a. Compile Error
b. Runtime Exception
c. 10 , [10,20,30]
d. 40 , [40,20,30]
e. None of the above
8. Choose the correct answer from the list of answers for the following question.
class A {
void m1(A a) {
System.out.println("A");
}
}
class B extends A {
void m1(B b) {
System.out.println("B");
}
}
class C extends B {
void m1(C c) {
System.out.println("C");
}
}
class D {
a. A , A , A
b. A , B , C
c. C , C , C
d.Compile Error
e.Runtime Error
9. Choose the correct answer(s) from the list of answers for the following question.
abstract class A {
class B extends A {
10. Study the question and select the correct answer from the answer list given below.
class A {
a. 2 , 3 , 4
b. 1 , 2 , 3 , 4
c. 1 , 3 , 4
d. 1 , 2 , 4
e. Compile Error
11. Choose the correct answer from the list of answers for the following question.
class A {
12. Select the correct answer to the following question from the answer list specified below.
class A {
boolean b = true;
if (b = true) { //Line 1
System.out.println("1");
}
if (b == false) //Line 2
System.out.println("2");
System.out.println("3");
}
}
13. Select the correct answer to the following question from the answer list specified below.
class A {
a. Compile Error
b. ABC
c. XYZ
d. DEF
e. DEF , XYZ
14. Select the correct answer to the following question from the answer list specified below.
class Vehicle {
void engine() {
System.out.println("Lorry Engine");
}
}
Lorry() {
}
}
a. Compile Error
b. Lorry Engine
c. Lorry Engine , Lorry Engine
d. ClassCastException at Runtime
e. None of the above
a. A , B , C
b. B , C , D
c. All
d. B , C , D , E
e. D , E , G , H
16. Choose the correct answer from the list of answers for the following question.
class A {
A(byte b) {
System.out.println("A");
}
}
class B extends A {
B(int i) {
this("B int");
System.out.println("C");
}
B(String s) {
super((byte) 100);
System.out.println("B");
}
18. Study the question and select the correct answer from the answer list given below.
class A {
A getA() {
System.out.println("Get A");
return new A();
}
a. Memory Location
b. Get A
c. Get A , Memory Location
d. null
e. Compile Error
19. Choose the correct answer from the list of answers for the following question.
class Car {
Tyre t = new Tyre();
}
class Tyre {
void tyreSize() {
System.out.println("Large");
}
}
20. Select the correct answer to the question from the answer list specified below.
class A {
A(int i) { } //Line 1
}
class A {
a. 1020 , 2147
b. Compile Error
c. 20 , 2147
d. 1020 , 257
e. Runtime Exception
22. Choose the correct answer from the list of answers for the following question.
class A {
a. ABC101010
b. ABC303030
c. ABC301030
d. ABC103010
e. None of the above
23. Choose the correct answer from the list of answers for the following question.
class Animal {
}
a. A
b. A , B
c. All are correct
d. All are incorrect
e. A , B , C
f. B , C , D
24. Select the correct answer to the following question from the answer list specified below.
class A {
int i = 50;
a. Compile Error
b. Runtime Exception
c. 500 , 300 , 200
d. 500 , 50 , 200
e. 300 , 300 , 200
25. Select the correct answer to the following question from the answer list specified below.
interface I1 {}
interface I2 {}
class Test {
26. Study the question and select the correct answer from the answer list given below.
class A {
void m(int i) {
System.out.println("A-int");
}
void m(short s) {
System.out.println("A-short");
}
}
class B extends A {
void m(long l) {
System.out.println("B-long");
}
a. A-short , A-int
b. A-short
c. B-long
d. A-short , A-int , B-long
e. Compile Error
27. Select the correct answer to the following question from the answer list specified below.
class A {
a. Compile Error
b. 25
c. 25.0
d. 25.00
e. None of the above
28. Choose the correct answer from the list of answers for the following question.
class A {
29.Select the correct answer to the following question from the answer list specified below.
class A { }
class B extends A {
30. Choose the correct answer from the list of answers for the following question.
class A {
byte b = 10;
char c = 'a';
boolean b1 = true;
int i = 20;
String s = null;
}
}
BigLongStringName
DogColour Red
$int
bytes
$i
finalist
Float
a. 1
b. 2
c. 4
d. 2 , 4
e. 1 , 2 , 3
32. Study the question and select the correct answer(s) from the answer list given below.
class A {
System.gc();
System.out.println("1");
a = null;
System.gc();
a1 = null;
System.gc();
System.out.println("2");
}
}
a. Compile Error
b. 1,Garbage Collected,2,Garbage Collected
c. 1,Garbage Collected,Garbage Collected,2
d. 1,2,Garbage Collected,Garbage Collected
e. Output can't Guaranteed.
33. Choose the correct answer from the list of answers for the following question.
class A {
int i = 10;
}
class B extends A {
int i = 20;
class C {
A a = new A();
B b = new B();
A a1 = new B();
a. Compile Error
b. true,true,true,true,true
c. true,true,true,true,true
d. true,true,true,false,true
e. true,true,true,true,false
34. Choose the correct answer from the list of answers for the following question.
class A {
A() {
System.out.println(C.i++);
}
A(int i) {
System.out.println(C.i++);
}
}
class B extends A {
B() {
C.i++;
System.out.println(C.i++);
}
B(byte b) {
this();
System.out.println(++C.i);
C.i++;
}
}
class C extends B {
static int i;
C() {
super((byte)10);
i++;
System.out.println(i);
}
C c = new C();
}
}
a. Runtime Exception
b. 0,2,3,6
c. 0,2,4,6
d. 1,2,3,6
e. 1,2,3,5
35. Which one of these lists contains only Java programming language keywords?
36. Which of the following declarations are not according to the Java Code Conventions
a. class Animal{}
b. public void GetName(){}
c. final int MAX_AGE = 10 ;
d. interface Runable{}
37. What will be the output of the following code? (ASCII value of ‘a’=97)
class A {
a. a
b. 97
c. Compile Error
d. Runtime Error
e. None of the above
class A {
a. No visible output
b. XYZ
c. Compile Error
d. ABC
e. None of the above
40. Which of the following correctly express the precedence of the compound operation x*=2+5; ?
a. x=(x*2)+5
b. x=x+2+5
c. x=2+(x*5)
d. x=x*(2+5)
class A {
a. 23
b. 202325
c. 2325Default
d. 2325
e. Compile Error
class A {
int m(int x) {
return x;
}
a. 25
b. 25.0
c. Compile Error at Line 1
d. Compile Error at Line 2
e. Runtime Exception
abstract class A {
void m2() {
System.out.println("m2-A");
}
}
class B extends A {
void m2() {
System.out.println("m2-B");
}
a. m2-A
b. m2-B
c. Compile Error
d. m2-A , m2-B
interface I {
class A implements I {
class A {
class B {
B() {
this(10);
System.out.println("X");
}
B(int x) {
this();
System.out.println("Y");
}
a. Compile Error
b. Runtime Exception
c. X , Y
d. Y , X
e. None of the above
50. Study the question and select the correct answer from the answer list given below.
class A extends B {
A() {
System.out.println("A");
}
}
class B extends A {
B() {
System.out.println("B");
}
}
class Test {
A a = new A();
B b = new B();
}
}
a. A , B
b. B , A
c. Compile Error
d. Runtime Exception
e. None of the above
51. Study the question and select the correct answer from the answer list given below.
class Test {
int x = 10;
x+=10;
x++;
System.out.println(x);
x--;
System.out.println(x++);
System.out.println(x++);
System.out.println(--x);
}
}
a. 21 , 21 , 21 , 21
b. 21 , 20 , 21 , 21
c. Compile Error
d. Runtime Exception
e. None of the above
52. Study the question and select the correct answer from the answer list given below.
class A{
A a = new A();
System.out.println(a.x); //Line 1
B b = new B();
System.out.println(b.x); //Line 2
}
}
a. 20 , 20
b. Compile Error at Line 2
c. Compile Error at Line 1
d. Runtime Exception
e. None of the above
53. Study the question and select the correct answer from the answer list given below.
class X {
System.out.println(o[2]);
}
}
a. A
b. Compile Error at Line 1
c. Compile Error at Line 2
d. Compile Error at Line 3
e. Runtime Exception
54. Choose the correct answer from the list of answers for the following question.
class Test{
int i = 20;
float f = i; //Line 1
System.out.println(f);
long l = f; //Line 2
System.out.println(l);
}
}
a. 20.0 , 20
b. 20 , 20
c. Compile Error at Line 1
d. Compile Error at Line 2
e. Runtime Exception
55. Choose the correct answer from the list of answers for the following question.
class Test {
int x = 0;
int i = 0;
do {
System.out.println("A");
x++;
} while (i == x);
System.out.println("B");
}
}
a. A
b. B
c. A , B
d. Compile Error
e. Runtime Exception
56. Choose the correct answer from the list of answers for the following question.
class A {
A a = new A();
a.finalize();
a = null;
System.gc();
}
}
a. Compile Error
b. null
c. A
d. A , A
e. Runtime Exception
57. Choose the correct answer from the list of answers for the following question.
class A {
class X {}
class Y extends X {}
58. Choose the correct answer from the list of answers for the following question.
interface X {
void m(); //Line 1
}
A a = new A();
a.n();
}
}
class X {}
class Y extends X {}
59. Choose the correct answer from the list of answers for the following question.
class A{
a. Compile Error
b. Aa , a10
c. A97 , 107
d. Aa , 107
e. Runtime Exception
60. Choose the correct answer from the list of answers for the following question.
class A{
A.m(10); //Line 1
A.m(10,20); //Line 2
A.m(10,20,30); //Line 3
}
Question 01
i. Is – A relationship
ii. Has – A relationship
iii. Polymorphism
iv. Encapsulation
Question 02
Question 03
Question 04
class X {
X() {
m();
System.out.println("X");
}
X(int i) {
this();
System.out.println("X int");
}
Y() {
super(20);
System.out.println("Y");
}
Y(int i) {
this();
System.out.println("Y int");
}
}
class Test {
Question 05
Section B Answers :
Q1:
1.
class A {
}
class B extends A {
}
2.
class A {
B b = new B();
}
class B {
public static void main(String[] args) {
A a = new A();
}
}
3.
Polymorphism :
class A {
}
class B extends A {
}
class Animal {
public void sound() {
System.out.println("Animal is making a sound");
}
}
class Horse extends Animal {
public void sound() {
System.out.println("Neigh");
}
public static void main(String args[]) {
Animal obj = new Horse();
obj.sound();
}
}
4.
class Account{
Q2 :
a)
b)
● Nulling Reference
● Reassigning
● local
● Isolated Island
● Nulling Reference :
class Garbage {
public static void main(String[] args) {
Garbage b1 = new Garbage();
System.out.println(b1);
b1 = null;
System.out.println(b1);
}
}
● Reassigning :
class Garbage {
System.out.println(b1);
b1 = new Garbage();
System.out.println(b1);
● Local
class Garbage {
static void m() {
Garbage b1 = new Garbage();
System.out.println(b1);
}
public static void main(String[] args) {
m();
}
}
● Isolated Island
class A {
A a;
public static void main(String[] args) {
A a1=new A();
A a2=new A();
A a3=new A();
a1.a=a2;
a2.a=a3;
a3.a=a1;
a1=null;
a2=null;
a3=null;
}
}
c)
class A {
int a;
@Override
public void finalize() {
System.out.println("Deleted..:" + a);
}
}
class Demo {
public static void main(String args[]) {
A a1 = new A();
a1.a=100;
a1 = null;
try {
Thread.sleep(1000);
} catch (Exception e) {
}
System.gc();
}
}
● An object is created in the memory using new operator.
● Constructor is used to initialize the properties of that object.
● When an object is no more required, it must be removed from the memory so that that memory can be reused for other objects.
● Removing unwanted objects or abandoned objects from the memory is called garbage collection (GC).
● In this code Garbage Collector clean abandoned objects, Garbage Collector calls finalize method automatically.
Q3 .
a)
Constructor is a block of code that initializes the newly created object.
A constructor resembles an instance method in java but it’s not a method as it doesn’t have a return type.
In short constructor and method are different(More on this at the end of this guide).
b)
class A {
A() {
System.out.println("A");
}
}
class B extends A {
B() {
super(); //== 1
System.out.println("B");
}
public static void main(String[] args) {
B b = new B();
}
}
● The super keyword in Java is a reference variable which is used to refer immediate parent class object.
● Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.
● Usage of Java super Keyword
● super can be used to refer immediate parent class instance variable.
● super can be used to invoke immediate parent class method.
● super() can be used to invoke immediate parent class constructor.
class A {
A() {
this(20); //=== 1
System.out.println("A1");
}
A(int i) {
super(); //==2
System.out.println("A2");
}
● “this” keyword can be used inside the constructor to call another overloaded constructor in the same Class.
● It is called the Explicit Constructor Invocation.
● This occurs if a Class has two overloaded constructors, one without argument and another with the argument.
● Then “this” keyword can be used to call the constructor with an argument from the constructor without argument.
c)
Constructor vs Methods
Constructor Method
A Constructor is a block of code that initializes a newly A Method is a collection of statements which returns a value upon its
created object execution.
A Constructor can be used to initialize an object. A Method consists of Java code to be executed.
A Constructor is invoked when a object is created using A Method is invoked through method calls.
the keyword new.
A Constructor doesn’t have a return type A Method must have a return type
A Constructor initializes a object that doesn’t exist. A Method does operations on an already created object.
A Constructor’s name must be same as the name of the A Method’s name can be anything.
class
A class can have many Constructors but must not have A class can have many methods but must not have the same
the same parameters. parameters
d)
Draw a constructor Chain
Q 5:
class A {
(b). Write a Java program to print numbers between 1 to 100 which are divisible by 3, 5 and by both.
class A {
c)
● Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
● Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the package.
● Protected: The access level of a protected modifier is within the package and outside the package through child class.
● Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within
the package and outside the package.