Predict the output of the following Java Programs.
Program 1:
// filename Main.java
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);
Program 2:
// filename Test.java
class Test {
public static void main(String[] args) {
for(int i = 0; 1; i++) {
System.out.println("Hello");
break;
Program 3:
// filename Main.java
class Main {
public static void main(String args[]) {
System.out.println(fun());
int fun() {
return 20;
Program 4:
// filename Test.java
class Test {
public static void main(String args[]) {
System.out.println(fun());
static int fun() {
static int x= 0;
return ++x;
Program 5:
package main;
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);
Program 6:
package main;
// filename Main.java
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);
Program 7
// filename: Test.java
class Test {
// Declaring and initializing integer variable
int x = 10;
// Main driver method
public static void main(String[] args)
// Creating an object of class inside main()
Test t = new Test();
// Printing the value inside the object by
// above created object
System.out.println(t.x);
}:
Program 8:
// filename: Test.java
// Main class
class Test {
// Declaring and initializing variables
int y = 2;
int x = y + 2;
// main driver method
public static void main(String[] args)
// Creating an object of class inside main() method
Test m = new Test();
// Printing the value of x and y
// using above object created
System.out.println("x = " + m.x + ", y = " + m.y);
}
Program 9:
// filename: Test.java
// Main class
public class Test {
// Declaring and initializing integer with custom value
int x = 2;
// Constructor of this class
// Parameterized constructor
Test(int i) { x = i; }
// Main driver method
public static void main(String[] args)
// Creating object of class in main()
Test t = new Test(5);
// Printing the value
System.out.println("x = " + t.x);
Program 10:
// filename: Test2.java
// Class 1
// Helper class
class Test1 {
// Constructor of this class
Test1(int x)
// Print statement whenever this constructor is
// called
System.out.println("Constructor called " + x);
// Class 2
// Class contains an instance of Test1
// Main class
class Test2 {
// Creating instance(object) of class1 in this class
Test1 t1 = new Test1(10);
// Constructor of this class
Test2(int i) { t1 = new Test1(i); }
// Main driver method
public static void main(String[] args)
// Creating instance of this class inside main()
Test2 t2 = new Test2(5);
}
11.We can make a class abstract by
1.Declaring it abstract using the virtual keyword
2.Making at least one member function as virtual function
3.Making at least one member function as pure virtual function
4.Making all member function const
12.Find the output of the following program.
public class Solution{
public static void main(String[] args){
short x = 10;
x = x * 5;
System.out.print(x);
1.50
2.10
3.COMPILE TIME
4.ERROR
13.Find the output of the following program.
public class Solution{
public static void main(String[] args){
byte x = 127;
x++;
x++;
System.out.print(x);
}
}
1.-127
2.127
3.129
4.2
14.Find the output of the following program.
public class Solution{
public static void main(String[] args){
int[] x = {120, 200, 016};
for(int i = 0; i < x.length; i++){
System.out.print(x[i] + “ “);
1.120 200 016
2.120 200 14
3.120 200 16
4.None
15.When an array is passed to a method, what does the method receive?
1.The reference of the array
2.A copy of the array
3.length of the array
4.copy of first element
1. Write a program to draw a Hollow rectangle star pattern
2. Write a program to draw a BUTTERFLY pattern
3. Write a program to draw a Solid Rhombus pattern.
4. Write a program to draw a hollow rhombus pattern.
5. Write a program to draw a Diamond Pattern.