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

Quiz About Java Class and Object I

Uploaded by

mostafa nasser
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Quiz About Java Class and Object I

Uploaded by

mostafa nasser
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Java Class and Object Trending in News V

105 Funny Things to Do to M


Question 1 Someone Laugh
Best PS5 SSDs in 2024: Top P
for Expanding Your Storage
Predict the output of following Java program?
Best Nintendo Switch Contr
in 2024
Java
#geekstreak2024 – 21 Days P
Challenge Powered By Deut
Bank
class Test {
int i; Full Stack Developer Roadm
} [2024 Updated]
class Main {
public static void main(String args[]) {
Test t;
System.out.println(t.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;
}

public void print()


{
System.out.println ("a = " + a + " b = " + b + "\\n");
}
}

class Test
{

public static void main(String[] args)


{
demo obj1 = new demo();
demo obj2 = obj1;

obj1.a += 1;
obj1.b += 1;

System.out.println ("values of obj1 : ");


obj1.print();
System.out.println ("values of obj2 : ");
obj2.print();

}
}

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

Run time error

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

Predict the output of following Java program.

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

Predict the output of the following program.

Java

class First
{

void display()
{
System.out.println("Inside First");
}
}

class Second extends First


{

void display()
{
System.out.println("Inside Second");
}
}

class Test
{

public static void main(String[] args)


{
First obj1 = new First();
Second obj2 = new Second();

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

Predict the output of the following program.

Java

class Test
{
int a = 1;
int b = 2;

Test func(Test obj)


{
Test obj3 = new Test();
obj3 = obj;
obj3.a = obj.a++ + ++obj.b;
obj.b = obj.b;
return obj3;
}
public static void main(String[] args)
{
Test obj1 = new Test();
Test obj2 = obj1.func(obj1);

System.out.println("obj1.a = " + obj1.a + " obj1.b = " + obj1.b);


System.out.println("obj2.a = " + obj2.a + " obj1.b = " + obj2.b);

}
}

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

obj1 and obj2 refer to same memory address.

Question 6

Predict the output of the following program.

Java

class Test
{
int a = 1;
int b = 2;

Test func(Test obj)


{
Test obj3 = new Test();
obj3 = obj;
obj3.a = obj.a++ + ++obj.b;
obj.b = obj.b;
return obj3;
}

public static void main(String[] args)


{
Test obj1 = new Test();
Test obj2 = obj1.func(obj1);

System.out.println("obj1.a = " + obj1.a + " obj1.b = " + obj1.b);


System.out.println("obj2.a = " + obj2.a + " obj1.b = " + obj2.b);

}
}

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

obj1 and obj2 refer to same memory address.

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

True, True, False, False

True, False, True, False

True, True, False, True

True, False, False, False

None o the above.

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

What is the output of the following JAVA program ?

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

What is the output of the following JAVA program ?

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

Java uses threads to enable the entire environment to be ______.

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

You have completed 10/12 questions .


Your accuracy is 70%.

Last Updated : Mar 22, 2024

Take a part in the ongoing discussion View All Discussion

Company Languages DSA Data Web Python


About Us Python Data Science & Technologies Tutorial
Corporate & Communications Legal Java Structures
Address:- A-143, 9th Floor, In Media
ML HTML Python
C++ Algorithms CSS Programming
Sovereign Corporate Tower, Data Science
Sector- 136, Noida, Uttar Pradesh Contact Us PHP DSA for JavaScript Examples
With Python
(201305) | Registered Address:- K Advertise with GoLang Beginners TypeScript Python Projects
061, Tower K, Gulshan Vivante Data Science
us SQL Basic DSA ReactJS Python Tkinter
Apartment, Sector 137, Noida, For Beginner
GFG Corporate R Language Problems NextJS Web Scraping
Gautam Buddh Nagar, Uttar Solution
Machine
Pradesh, 201305 Android DSA Bootstrap OpenCV Tutorial
Learning
Placement Tutorial Roadmap Web Design Python Interview
ML Maths
Training Tutorials Top 100 Question
Data
Program Archive DSA Django
Visualisation
GeeksforGeeks Interview
Pandas
Community Problems
NumPy
DSA
NLP
Roadmap
Deep Learning
by
Sandeep
Jain
All Cheat
Sheets

Computer DevOps System Inteview School GeeksforGeeks


Science Git Design Preparation Subjects Videos
Operating Linux High Level Competitive Mathematics DSA
Systems AWS Design Programming Physics Python
Computer Docker Low Level Top DS or Algo Chemistry Java
Network Kubernetes Design for CP Biology C++
Database Azure UML Company-Wise Social Science Web Development
Management GCP Diagrams Recruitment English Data Science
System DevOps Interview Process Grammar CS Subjects
Software Roadmap Guide Company-Wise Commerce
Engineering Design Preparation World GK
Patterns
Digital Logic OOAD Aptitude
Design System Preparation
Engineering Design Puzzles
Maths Bootcamp
Software Interview
Development Questions
Software
Testing

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

You might also like