Core Java Technical Test – this keyword and this()

Core Java Technical Test is providing SCJP or OCJP certification level questions to test your Java programming skills and helps to clear all the technical interviews easily. This test is including the concept of this keyword and this() in Java.


1. What is the output of following code?
class Test
{
Test()
{
System.out.println(“One”);
}
Test(Test a)
{
System.out.println(“Two”);
}
public static void main(String[] args)
{
System.out.println(new Test(new Test()));
}
}

 
 
 
 

2. What is the output of following code?
class Test
{
Test()
{
this();
System.out.println(“Message”);
}
public static void main(String args[ ])
{
new Test();
}
}

 

 
 
 
 

3. What is the output of following code?
class Test
{
int a ;
Test(int x)
{
x = this.a ;
System.out.println(a);
}
public static void main(String args[ ])
{
System.out.println(new Test(10).a);
}
}

 
 
 
 

4. What is the output of following code?
class Test
{
static Test a ;
Test b ;
Test()
{
b = a ;
a.check();
}
void check()
{
System.out.println(“Check”);
}
public static void main(String[] args)
{
new Test();
}
}

 
 
 
 

5. What is the output of following code?
class Test
{
Test()
{
System.out.println(“Constructor”);
}
void check()
{
this();
System.out.println(“Check”);
}
public static void main(String[] args)
{
Test t = new Test();
t.check();
}
}

 
 
 
 

6. What is the output of following code?
class Test
{
int a ;
Test(int x)
{
a = x ;
}
public static void main(String args[ ])
{
Test x = new Test(10);
System.out.println(x.a);
}
{
System.out.println(this.a);
}
}

 
 
 
 

7. What is the output of following code?
class Test
{
Test(int x)
{
System.out.println(“One”);
}
Test()
{
this(10,20);
System.out.println(“Zero”);
}
Test(int x, int y)
{
this(10);
System.out.println(“Two”);
}
public static void main(String[] args)
{
new Test();
}
}

 

 
 
 
 

8. What is the output of following code?
class Test
{
static Test a ;
Test()
{
a = this ;
}
void check()
{
System.out.println(“Check”);
}
{
this.check();
}
public static void main(String[] args)
{
new Test();
System.out.println(a);
}
}

 
 
 
 

9. What is the output of following code?
class Test
{
int x;
Test(Test obj)
{
x = obj.x + 5 ;
}
Test(int x)
{
x = x ;
}
public static void main(String[] args)
{
System.out.println(new Test(new Test(10)).x);
}
}

 
 
 
 

10. What is the output of following code?
class Test
{
static Test a ;
Test b ;
Test()
{
b = a ;
}
{
a = this ;
}
public static void main(String[] args)
{
new Test();
System.out.println(a.b);
}
}

 
 
 
 

Question 1 of 10

Share this