Quiz About Java Class and Object I
Quiz About Java Class and Object I
garbage value
compiler error
runtime error
Discuss it
Question 1 ‒ Explanation
t is just a reference, the object referred by t is not allocated any memory. Unlike C++, in Java all
non-primitive objects must be explicitly allocated and these objects are allocated on heap. The
following is corrected program.
Java
class Test {
int i;
}
class Main {
public static void main(String args[]) {
Test t = new Test();
System.out.println(t.i);
}
Question 2
JAVA
class demo
{
int a, b;
demo()
{
a = 10;
b = 20;
}
class Test
{
obj1.a += 1;
obj1.b += 1;
}
}
Compile error
values of obj1:
a = 11 b = 21
values of obj2:
a = 11 b = 21
values of obj1:
a = 11 b = 21
values of obj2:
a = 10 b = 20
values of obj1:
a = 11 b = 20
values of obj2:
a = 10 b = 21
Discuss it
Question 2 ‒ Explanation
Assignment of obj2 to obj1 makes obj2 a reference to obj1. Therefore, any change in obj1 will
be reflected in obj2 also.
Question 3
Java
class demoClass
{
int a = 1;
void func()
{
demo obj = new demo();
obj.display();
}
class demo
{
int b = 2;
void display()
{
System.out.println("\\na = " + a);
}
}
void get()
{
System.out.println("\\nb = " + b);
}
}
class Test
{
public static void main(String[] args)
{
demoClass obj = new demoClass();
obj.func();
obj.get();
}
}
a = 1
b = 2
Compilation error
b = 2
a = 1
Discuss it
Question 3 ‒ Explanation
Members of inner class ‘demo’ can not be used in the outer class ‘Test’. Thus, get() of outer clas
s can not access variable ‘b’ of inner class.
Question 4
Java
class First
{
void display()
{
System.out.println("Inside First");
}
}
void display()
{
System.out.println("Inside Second");
}
}
class Test
{
First ref;
ref = obj1;
ref.display();
ref = obj2;
ref.display();
}
}
Compilation error
Inside First
Inside Second
Inside First
Inside First
Runtime error
Discuss it
Question 4 ‒ Explanation
‘ref’ is a reference variable which obtains the reference of object of class First and calls its functi
on display(). Then ‘ref’ refers to object of class Second and calls its function display().
Question 5
Java
class Test
{
int a = 1;
int b = 2;
}
}
obj1.a = 1 obj1.b = 2
obj2.a = 4 obj2.b = 3
obj1.a = 4 obj1.b = 3
obj2.a = 4 obj2.b = 3
Compilation error
Discuss it
Question 5 ‒ Explanation
Question 6
Java
class Test
{
int a = 1;
int b = 2;
}
}
obj1.a = 1 obj1.b = 2
obj2.a = 4 obj2.b = 3
obj1.a = 4 obj1.b = 3
obj2.a = 4 obj2.b = 3
Compilation error
Discuss it
Question 6 ‒ Explanation
Question 7
What is the output of the following JAVA program? public class Good{ Private int m; Public Good(int
m){this.m=m;} public Boolean equals(Good n){return n.m=m;} public static void main(String args [ ]){
Good m1=new Good(22); Good m2=new Good(22); Object S1=new Good(22); Object S2=new
good(22); System.out.println(m1.equals(m2)); System.out.println(m1.equals(s2));
System.out.println(m1.equals(s2)); System.out.println(s1.equals(m2)); } }
Discuss it
Question 7 ‒ Explanation
Note: Question is wrong. First change boolean to int data type then there is a possibility of opti
on 4 is correct. So marks will be added to all students. GeeksforGeeks code link: https://ide.gee
ksforgeeks.org/tWbTXjT2VA Output: true false false false
Question 8
class simple
{
public static void main(String[ ] args)
{
simple obj = new simple( );
obj.start( );
}
void start( )
{
long [ ] P= {3, 4, 5};
long [ ] Q= method (P);
System.out.print (P[0] + P[1] + P[2]+”:”);
System.out.print (Q[0] + Q[1] + Q[2]);
}
long [ ] method (long [ ] R)
{
R [1]=7;
return R;
}
} //end of class
12 : 15
15 : 12
12 : 12
15 : 15
Discuss it
Question 8 ‒ Explanation
When above program compliled and run on ide then it will produce 15:15. IDE link Option (D) is cor
rect.
Question 9
Java
Class Test {
public static void main(String[] args) {
Test obj = new Test();
obj.start();
}
void start() {
String stra = ”do”;
String strb = method(stra);
System.out.print(“: ”+stra + strb);
}
String method(String stra) {
stra = stra + ”good”;
System.out.print(stra);
return“ good”;
}
}
dogood : dogoodgood
dogood : gooddogood
dogood : dodogood
dogood : dogood
Discuss it
Question 10
Symmetric
Asymmetric
Synchronous
Asynchronous
Discuss it
Question 10 ‒ Explanation
Java uses threads to enable the entire environment to be asynchronous. Asynchronous threadi
ng is pre-emptive i.e. a thread once start executing a task it can hold it in mid, save the current
state and start executing another task (context switching) according to priority and other specif
ied criteria and threading. So, option (D) is correct.
1 2