0% found this document useful (0 votes)
8 views

Oopc I Paper 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Oopc I Paper 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Section A

1. Select the correct answer to the following question from the answer list specified below.

class A {

static int x = 10;

public static void main(String[] args) {

A a = new A();
System.out.println(a.x); //Line 1
System.out.println(A.x); //Line 2

}
}

a. Compile Error at Line 1


b. Compile Error at Line 2
c. 10 , 10
d. Runtime Exception
e. null , 10

2. Choose the correct answer from the list of answers for the
following question.

class Dog {

final String name;

public Dog(String name) {


this.name = name;
}

public static void main(String args[]) {


Dog d = new Dog("Scooby"); //Line 1
System.out.println(d.name);
d.name = "Snowy"; //Line 2
System.out.println(d.name);
}
}

a. Compile Error at Line 1


b. Compile Error at Line 2
c. Scooby , Snowy
d. Scooby , Scooby
e. Snowy , Snowy

3. Select the correct answer to the following question from


the answer list specified below.

class A {

int x = 10;
int y = 20;
int z = 30;

public static void main(String[] args) {

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 {

public static void main(String[] args) {


char a = 'a'; // 'a' = 97
char b = 'b'; // 'b' = 98
System.out.print(a + b + "" + a + b);
}
}

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 {

final void m1(); //Line 1

synchronized void m2(); //Line 2

public void m3(); //Line 3

native void m4(); //Line 4


}

a. Compile Error at Line 1


b. Compile Error at Line 2
c. Compile Error at Line 3
d. Compile Error at Line 4
e. None of the above

5. Select the correct answer to the following question from the answer list specified below.

class Gun {

BulletCount b;

public void setBullet() {


b = new BulletCount();
b.count = 50;
System.out.println("Fire");
}

public static void main(String[] args) {


new Gun().setBullet();
}
}

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 {

static int x = 10;


int y = 20;

public static void main(String[] args) {


System.out.println(x); //Line 1
System.out.println(y); //Line 2
}
}

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 {

public static void main(String[] args) {

int x[] = new int[3];


x[1] = 10;
x[2] = 20;
x[3] = 30;
x[1] = 40;

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 {

public static void main(String[] args) {


A a1 = new A();
B b1 = new B();
C c1 = new C();
A c2 = new C();
c2.m1(a1);
c2.m1(b1);
c2.m1(c1);
}
}

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 {

public void m() {


System.out.println("m in abstract class A");
}

abstract void m2();

abstract void m3();


}

class B extends A {

public static void main(String[] args) {


A a = new A();
a.m();
}
}

a. Compile Error - abstract classes cannot be a super class.


b. Compile Error - abstract method must has a body.
c. Compile Error - abstract methods must be overridden in the first subclass.
d. Compile Error - abstract classes can not be instantiated.

10. Study the question and select the correct answer from the answer list given below.

class A {

public static void main(String args[]) {


char c = 'b';
switch (c) {
case 'a':
System.out.print("1");
case 'b':
System.out.print("2");
case 'c':
System.out.print("3");
default:
System.out.print("4");
}
}
}

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 {

public static void main(String[] args) {

byte b = (byte) 200; // Line 1


int i1 = b; // Line 1
System.out.println(i1);
if (true) {
int i2 = 100;
b = i2; // Line 3
System.out.println(i2);
}
}
}

a. Compile Error at Line 1


b. Compile Error at Line 2
c. Compile Error at Line 3
d. 200 , 100
e. 200 , 200

12. Select the correct answer to the following question from the answer list specified below.

class A {

public static void main(String args[]) {

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");
}
}

a. Compile Error at Line 1


b. Compile Error at Line 2
c. 1 , 2 , 3
d. 1 , 3
e. 1

13. Select the correct answer to the following question from the answer list specified below.

class A {

public static void main(String[] args) {


String s = (4 > 5) ? ("ABC") : ((5 > 6) ? ("DEF") : ("XYZ"));
System.out.println(s);
}
}

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");
}
}

class Lorry extends Vehicle {

Lorry() {
}
}

class DemoBatta extends Lorry {

public static void main(String[] args) {


DemoBatta DB = new DemoBatta();
DB.engine();
}
}

a. Compile Error
b. Lorry Engine
c. Lorry Engine , Lorry Engine
d. ClassCastException at Runtime
e. None of the above

15. What are the correct rules of overriding?

A. Argument list should not exactly match


B. Same or wider access level
C. Same or narrower Checked Exceptions
D. Instance methods can be overridden only if they are inherited by the subclass.
E. ​The return type must be the same as, or a subtype of, the return type declared in the original overridden method in the supercla​ss
G. Can override a method marked final.
H. Can override a method marked static.

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");
}

public static void main(String[] args) {


A a = new B(10);
}
}
a. A , B , C , B int
b. A , B , C
c. Compile Error
d. B , C
e. Runtime Error

17. What are the correct statements(java 7.0)? [Select 3]


a.interface methods must be static.
b. An interface can extend with only one interface.
c. interface methods are abstract, they cannot be marked final, strictfp, or native.
d. An interface cannot extend anything but another interface​.
e. An interface can implement another interface or class.
f. An interface must be declared with the keyword interface.

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();
}

public static void main(String[] args) {


System.out.println(A.getA());
A a = 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 Audi extends Car {

public static void main(String[] args) {


Car c = new Audi(); //Line 1
c.t.tyreSize(); //Line 2
}
}

class Tyre {

void tyreSize() {
System.out.println("Large");
}
}

a. Compile Error at Line 1


a. Compile Error at Line 2
b. Runtime Exception
c. Large
d. None of the above

20. Select the correct answer to the question from the answer list specified below.

class A {

A(int i) { } //Line 1
}

class B extends A {} //Line 2

Which of the following statements are true? [Choose 2]

a. The compiler attempts to create a default constructor for class A


b. The compiler attempts to create a default constructor for class B
c. Compiler error at 1
d.​ Compiler error at 2
e. None of the above
21. Choose the correct answer from the list of answers for the following question.

class A {

static int p = 100;


static byte q = 127;

public static void main(String[] args) {


p = 1000;
long r = A.p;
short s = 20;
System.out.println(r + s);
r = p + q + r + s;
System.out.println(r);
}
}

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 {

public static void main(String[] args) {


int i = 10;
String s = "ABC";
byte b = 30;
short c = b;
i = c;
System.out.println(s + i + b + c);
}
}

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 {
}

class Mammal extends Animal {


}

class Reptile extends Animal {


}

class Dog extends Mammal {


}

What are the True Answers?

A. Animal is the super class of Mammal class.


B. Animal is the super class of Reptile class.
C. Mammal and Reptile are subclasses of Animal class.
D. Dog is the subclass of both Mammal and Animal classes.

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;

public static void main(String[] args) {


A a1 = new A();
A a2 = a1;
A a3 = new A();
a1.i = 500;
a2.i = 300;
a3.i = 200;
System.out.println(a1.i);
System.out.println(a2.i);
System.out.println(a3.i);
}
}

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 Base implements I1 {}

class Sub extends Base implements I2 {} //Line 1

class Test {

public static void main(String args[]) {


Base base = new Base();
I1 i1 = base; //Line 2
Sub sub = (Sub) base; //Line 3
}
}

a. Compiler Error at Line 1


b. Compiler Error at Line 2
c. Compiler Error at Line 3
d. Runtime Exception at Line 2
e. Runtime Exception at Line 3

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");
}

public static void main(String[] args) {


byte b = 10;
A a = new B();
a.m(b);
}
}

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 {

public static void main(String[] args) {


int i;
double x = 25;
System.out.println(x);
}
}

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 {

void m(String a, int i) {


System.out.println(++i + " " + a);
}

void m1(String a, String b) {


System.out.println(a + " " + b);
}

public static void main(String[] args) {


A a = new A();
a.m("A", 0);
a.m1("A", "B");
}
}

a. Compile Error At Line 1


b. 1 A , A , B
c. 0 A, A , B
d. Runtime Exception
e. None of the above

29.Select the correct answer to the following question from the answer list specified below.

class A { }

class B extends A {

public static void main(String[] args) {


A a = new A(); // Line 1
A ab = new B(); // Line 2
B b = new B(); // Line 3
B ba = new A(); // Line 4
Object o = new A(); // Line 5
}
}

a. Compile Error At Line 1


b. Compile Error At Line 2
c. Compile Error At Line 3
d. Compile Error At Line 4
e. Compile Error At Line 5

30. Choose the correct answer from the list of answers for the following question.

class A {

public static void main(String[] args) {

byte b = 10;

char c = 'a';

boolean b1 = true;

int i = 20;

String s = null;

System.out.println(s += b); // Line 1


s += c; // Line 2
s += b1; // Line 3
System.out.println(s += i); // Line 4

}
}

a. Compile Error At Line 1


b. Compile Error At Line 2 & Line 3
c. Compile Error At Line 4
d. java.lang.NullPointerException
e. null10,null10atrue20

31. Choose the invalid identifiers from those listed below.

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 {

public void finalize() {


System.out.println("Garbage Collected");
}
public static void main(String args[]) {
A a = new A();
A a1 = new 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 {

public static void main(String[] args) {

A a = new A();
B b = new B();
A a1 = new B();

System.out.println(a instanceof A);


System.out.println(b instanceof A);
System.out.println(a1 instanceof A);
System.out.println(b instanceof Object);
System.out.println(a1 instanceof 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);
}

public static void main(String[] args) {

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?

a. class, if, void, long, string, continue


b. final, try, private, while, char, extends
c. try, virtual, throw, final, volatile, transient
d. strictfp, constant, super, implements, do

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 {

public static void m(char c) {


System.out.println(c);
}

public static void main(String[] args) {


m(97);
}
}

a. a
b. 97
c. Compile Error
d. Runtime Error
e. None of the above

38.Select the incorrect statement(s).

a. Local variables cannot be accessed outside their scope.


b. Local variables can be accessed without initializing.
c. Static variable can be accessed without initializing.
d. Both static variables and instance variables are having a default value.

39. What is the output of the following code?

class A {

public static A m() {


return null;
}
public static void main(String[] args) {
if (m() == null) {
System.out.println("ABC");
} else {
System.out.println("XYZ");
}
}
}

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)

41. What is the output of following code?

class A {

public static void main(String[] args) {


int x = 23;
switch (x) {
case 20:
System.out.println("20");
case 23:
System.out.println("23");
case 25:
System.out.println("25");
break;
default:
System.out.println("Default");
}
}
}

a. 23
b. 202325
c. 2325Default
d. 2325
e. Compile Error

42. Select the correct statement(s)

a. Constructor name must be same as its class name.


b. Constructor must not have explicit return type.
c. Default Constructor has arguments in the parameter list.
d. Constructors cannot be overloaded.

43. Select the correct statement(s)

a. Java has three Access Modifiers.


b. Private members of a class can be accessed inside the sub classes of the particular class.
c. We can apply private modifier to modify a class declaration.
d. Method local variables can be modified by the private Keyword.

44. What is the output of the following code?

class A {

int m(int x) {
return x;
}

double m(int x) { //Line 1


return x;
}
public static void main(String[] args) {
System.out.println(new A().m(25)); //Line 2
}
}

a. 25
b. 25.0
c. Compile Error at Line 1
d. Compile Error at Line 2
e. Runtime Exception

45. What is the output of the following code?

abstract class A {

abstract void m1();

void m2() {
System.out.println("m2-A");
}
}

class B extends A {

void m2() {
System.out.println("m2-B");
}

public static void main(String[] args) {


B b = new B();
b.m2();
}
}

a. m2-A
b. m2-B
c. Compile Error
d. m2-A , m2-B

46. What is the incorrect statement(s) about java overriding?

a. You cannot override a method marked static.


b. You cannot override a method marked final.
c. Instance methods can be overridden even if they are not inherited by the subclass​.
d. Argument list must exactly match.

47. What is the output of the following code?

interface I {

void m(); //Line 1


}

class A implements I {

void m() { //Line 2


System.out.println("A");
}

public static void main(String[] args) {


new A().m(); //Line 3
}
}

a. Compile Error at line 01


b. A
c. Compile Error at line 02
d. Compile Error at line 03
48. Select the correct statement(s) about the following code.

class A {

private int x = 10;

public int getX() {


return x;
}

public void setX(int x) {


this.x = x;
}
}

a. Variable x can be accessed from a sub class of class A.


b. This is a tightly encapsulated class.
c. This is not a tightly encapsulated class.
d. Variable x can be accessed inside the class without using the getter and setter.

49. What is the output of the following code?

class B {

B() {
this(10);
System.out.println("X");
}

B(int x) {
this();
System.out.println("Y");
}

public static void main(String[] args) {


B b = new B();
}
}

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 {

public static void main(String[] args) {

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 {

public static void main(String[] args) {

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{

private int x = 20;


}
class B extends A{

public static void main(String[] args) {

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 {

public static void main(String[] args) {

Object o[] = new Object[3];


o[0] = new Object(); //Line 1
o[1] = new X(); //Line 2
o[2] = "A"; //Line 3

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{

public static void main(String[] args) {

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 {

public static void main(String[] args) {

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 {

protected void finalize() {


System.out.println("A");
}

public static void main(String[] args) {

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 {

public static void main(String[] args) {


X x1 = new X();
X x2 = new Y();
Y y1 = new Y();
x2 = y1; //Line 1
y1 = (Y) x2; //Line 2
}
}

class X {}

class Y extends X {}

a. Compile Error at Line 1


b. Compile Error at Line 2
c. Runtime Exception
d. None of the above

58. Choose the correct answer from the list of answers for the following question.

interface X {
void m(); //Line 1
}

abstract class Y implements X {

public void m() {


System.out.println("m");
}

void n(); //Line 2


}

class A extends Y{ //Line 3

public static void main(String[] args) {

A a = new A();
a.n();
}
}

class X {}

class Y extends X {}

a. Compile Error at Line 1


b. Compile Error at Line 2
c. Compile Error at Line 3
d. Runtime Exception
e. None of the above

59. Choose the correct answer from the list of answers for the following question.

class A{

public static void main(String[] args) {

char c = 97; //ASCII 97 = a


String s = "A";
System.out.println(s+c);
System.out.println(c+10);
}
}

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{

public static void main(String[] args) {

A.m(10); //Line 1
A.m(10,20); //Line 2
A.m(10,20,30); //Line 3
}

public static void m(int ...x){ //Line 4


System.out.println(x[0]);
System.out.println(x[1]);
}
}

a. Compile Error at Line 1


b. Compile Error at Line 2
c. Compile Error at Line 3
d. Compile Error at Line 4
e. Runtime Exception
f. 10 , 0 , 10 , 20 , 10 , 20

Section B (Answer All Questions )

Question 01

(a). Write code examples to describe the following concepts,

i. Is – A relationship
ii. Has – A relationship
iii. Polymorphism
iv. Encapsulation

Question 02

(a). Describe the importance of Garbage Collection.


(b). What are the types of garbage collection? Explain with code examples.
(c). Explain the finalize() method in java using a code example.

Question 03

(a). Write a method Structure and explain it with example code.


(b). Describe the difference between Overriding and Overloading with code example.
(c). Explain the usage of return type Object and parameter list data type Object using code examples.

Question 04

(a). What is a constructor?


(b). Describe super(); and this(); using a code examples.
(c). Give five differences between a method and a constructor.
(d). Draw a constructor chain for the following code in your answer book.

class X {

X() {
m();
System.out.println("X");
}

X(int i) {
this();
System.out.println("X int");
}

public void m(){


System.out.println("m");
}
}
class Y extends X{

Y() {
super(20);
System.out.println("Y");
}

Y(int i) {
this();
System.out.println("Y int");
}
}

class Test {

public static void main(String[] args) {


Y y = new Y(10);
}
}

Question 05

(a). Print odd numbers between 10 and 100


(b). ​Write a Java program to print numbers between 1 to 100 which are divisible by 3, 5 and by both.
(c). Write all the access modifiers and describe briefly

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{

private int balance ; // Encapsulated Variable


private String name; // Encapsulated Variable

Q2 :

a)

● Java applications obtain objects in memory as needed.


● It is the task of garbage collection (GC) in the Java virtual machine (JVM) to automatically determine what memory is no longer
being used by a Java application and to recycle this memory for other uses.
● Because memory is automatically reclaimed in the JVM, Java application developers are not burdened with having to explicitly
free memory objects that are not being used.
● The GC operation is based on the premise that most objects used in the Java code are short-lived and can be reclaimed shortly after
their creation.
● Because unreferenced objects are automatically removed from the heap memory, GC makes Java memory-efficient.

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 {

public static void main(String[] args) {

Garbage b1 = new 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");
}

public static void main(String[] args) {


A b = new A();
}
}

● “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

A Constructor cannot be inherited by subclasses. A Method can be inherited by subclasses.

d)
Draw a constructor Chain

Q 5:

(a). Print odd numbers between 10 and 100

class A {

public static void main(String[] args) {


for (int i = 10; i < 101; i++)
{
if (i % 2 == 1) {
System.out.println(i);
}
}
}
}

(b). Write a Java program to print numbers between 1 to 100 which are divisible by 3, 5 and by both.

class A {

public static void main(String[] args) {


for (int i = 0; i < 101; i++)
{
if (i % 3 == 0&&i%5==0) {
System.out.println(i);
}
}
}
}

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.

You might also like