Predict the output of following Java program?
Java
class Test {
int i ;
}
class Main {
public static void main ( String args [] ) {
Test t ;
System . out . println ( t . i );
}
What is the output of the below Java code?
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 ();
}
}
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 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 ();
}
}
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 ();
}
}
Inside First
Inside Second
Inside First
Inside First
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
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
What is the output of the following Java program?
Java
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, False, False, False
What is the output of the following Java program?
Java
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 ;
}
}
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" ;
}
}
Java uses threads to enable the entire environment to be ______.
Master JAVA and also get 90% fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.
After successfully processing refunds worth over INR 5 Cr, GeeksforGeeks is back with the Three 90 challenge and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!
There are 12 questions to complete.
Take a part in the ongoing discussion View All Discussion