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

Predict the output of the following Java Programs

Uploaded by

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

Predict the output of the following Java Programs

Uploaded by

dgpguru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Predict the output of the following Java Programs.

1. class Test {
protected int x, y;
}
class Main {
public static void main(String args[]) {
Test t = new Test();
System.out.println(t.x + " " + t.y);
}}
2. class Test {

public static void main(String[] args) {


for(int i = 0; 1; i++) {
System.out.println("Hello");
break;
} }}
3. class Main {

public static void main(String args[]) {


System.out.println(fun());
}
int fun() {
return 20;
} }
4. class Test {

public static void main(String args[]) {


System.out.println(fun());
}
static int fun() {
static int x= 0;
return ++x;
}}
5. class Base {

public void Print()


{
System.out.println("Base");
}
}

class Derived extends Base {


public void Print()
{
System.out.println("Derived");
}
}

class Main {
public static void DoPrint(Base o)
{
o.Print();
}
public static void main(String[] args)
{
Base x = new Base();
Base y = new Derived();
Derived z = new Derived();
DoPrint(x);
DoPrint(y);
DoPrint(z);
} }

6. class Point {

protected int x, y;

public Point(int _x, int _y)


{
x = _x;
y = _y;
}}

public class Main {


public static void main(String args[])
{
Point p = new Point();
System.out.println("x = " + p.x + ", y = " + p.y);
}}
7. class Test {

int x = 10;
public static void main(String[] args)
{
Test t = new Test();
System.out.println(t.x);
}
}

8. public class Test {

int x = 2;
Test(int i) { x = i; }
public static void main(String[] args)
{
Test t = new Test(5);
System.out.println("x = " + t.x);
}
}
9. class Test1 {

Test1(int x)
{

System.out.println("Constructor called " + x);


}
}
class Test2 {
Test1 t1 = new Test1(10);
Test2(int i) { t1 = new Test1(i); }
public static void main(String[] args)
{
Test2 t2 = new Test2(5);
} }
10. class Base {
protected void foo() {}
}
class Derived extends Base {
void foo() {}
}
public class Main {
public static void main(String args[]) {
Derived d = new Derived();
d.foo();
}
}
11. class Complex {
private double re, im;
public String toString() {
return "(" + re + " + " + im + "i)";
}
Complex(Complex c) {
re = c.re;
im = c.im;
}
}

public class Main {


public static void main(String[] args) {
Complex c1 = new Complex();
Complex c2 = new Complex(c1);
System.out.println(c2);
}

You might also like