JAVA – OUTPUT BASED QUESTIONS (WORKSHEET 1)
By S. Mukherjee
[**Questions have been taken from various sources and the answers provided are mostly correct.)
1) void test4(String x, String y)
{
   if(x.compareTo(y) > 0)
      System.out.println(x);
   else
      System.out.println(y);
}
if "AMIT" and "AMAN" are passed to the function.
2)
System.out.println("This is my first Java program");
System.out.print("Display the message:");
System.out.print("Have fun!!!");
3) char ch = 'F';
int m = ch;
m=m+5;
System.out.println(m + " " + ch);
5) char x, y;
x = 'y';
System.out.println(x);
y = 'z';
System.out.println(y);
x = y;
System.out.println(x);
6)
void test1(int n)
{
for(int x=1; x<=n; x++)
if(n%x == 0)
   System. out.println(x);
}
if 12 is passed to n.
7) void test2(int a, int b)
{
  while( a != b)
  {
    if ( a > b)
      a = a — b;
    else
      a = b — a;
  }
  System.out.println(a);
}
if 4 and 17 are passed to the function.
8) int y,p;
for (int x=1; x<=3; x++)
{
for (y=1; y<=2; y++)
{
p = x * y;
System.out.print(p);
}
System.out.println( );
}
9) int x,y;
for(x=1; x<=5; x++)
{
for(y=1; y<x; y++)
{
if(x == 4)
   break;
   System.out.print(y);
}
System.out.println( );
}
10) int a,b;
for (a=1; a<=2; a++)
{
for (b= (64+a); b<=70; b++)
System.out.print((char) b);
System.out.println( );
}
11) int i,j;
first:
for (i=10; i>=5; i--)
{
for (j= 5; j<=i; j++)
{
if (i*j <40)
   continue first;
System.out.print(j);
}
System.out.println( );
}
12)
int i,j;
for (i=0; i<4; i++)
{
for (j=i; j>=0; j--)
System.out.print(j);
System.out.println();
}
13) int m=3,n=5,p=4;
if(m==n && n!=p)
{
System.out.println(m*n);
System.out.println(n%p);
}
if((m!=n) || (n==p))
{
System.out.println(m+n);
System.out.println(m-n);
}
14) int a=1,b=2,c=3;
switch(p)
{
case 1: a++;
case 2: ++b;
break;
case 3: c--;
}
System.out.println(a + ","
+ b + "," +c);
15) int a=1,b=1,m=10,n=5;
if((a==1)&&(b==0))
{
System.out.println((m+n));
System.out.println((m—n));
}
if((a==1)&&(b==1))
{
System.out.println((m*n));
System. out.println((m%n));
}
16) int b=3,k,r;
float a=15.15,c=0;
if(k==1)
{
r=(int)a/b;
System.out.println(r);
}
else
{
c=a/b;
System.out.println(c);
}
17) int x=1,y=1;
if(n>0)
{
x=x+1;
y=y+1;
}
What will be the value of x and y, if n assumes a value (i) 1 (ii) 0?
18)
switch (opn)
{
case 'a':
System.out.println("Platform Independent");
break;
case 'b':
System.out.println("Object Oriented");
case 'c':
System.out.println("Robust and Secure");
break;
default:
System.out.println("Wrong Input");
}
When (i) opn = 'b' (ii) opn = 'x' (iii) opn = 'a'
19)
int a=10,b=12;
if(a==10&&b<=12)
a--;
else
++b;
System.out.println(a + "and" +b);
20) int a=10,b=12;
if(a>=10)
a++;
else
++b;
System.out.println(a + "and" +b);
21) class Test {
  public static void main(String args[]) {
     int i;
     for(i=0;i<5;i++)
       System.out.println(i-i*i);
  }
}
22) int b=3,k,r;
float a=15.15,c=0;
if(k==1)
{
r=(int)a/b;
System.out.println(r);
}
else
{
c=a/b;
System.out.println(c);
23) int x = 1,y = 1;
if(n>0)
{
x=x+1;
y=y+1;
}
What will be the value of x and y, if n assumes a value (i) 1 (ii) 0?
24) int a=1,b=1,m=10,n=5;
if((a==1)&&(b==0))
{
System.out.println((m+n));
System.out.println((m-n));
}
if((a==1)&&(b==1))
{
System.out.println((m*n));
System.out.println((m%n));
}
25) int a[] ={2,4,6,8,10};
a[0]=23;
a[3]=a[1];
int c= a[0]+a[1];
System.out.println("Sum = "+c);
26) int a[4]={2,4,6,8};
for(i=0;i<=1;i++)
{
s=a[i]+a[3-i];
System.out.println(s);
}
27) int n[] ={1,2,3,5,7,9,13,16};
double a=Math.pow(n[4], n[1]);
double b=Math.sqrt(n[5]+n[7]);
System.out.println("a=" + a);
System.out.println("b=" + b);
28) int x[] = {4, 3, 7, 8, 9, 10};
int p = x.length;
int q = x[2] + x[5] * x[1];
System.out.println("p=" + p);
System.out.println("q=" + q);
29) System.out.println(Math.max(Math.ceil(14.55),15.5));
30) public class A {
        public static void main(String[] args)
        {
          if (true)
         break;
        }
}
  ans) error (break outside switch or loop)
31) public class A {
        public static void main(String[] args)
        {
        System.out.println('j' + 'a' + 'v' + 'a');
        }
}                                                    options : a) java b)something else(mention what)
32) public class A {
         public static void main(String[] args)
         {
    int $_ = 5;
    System.out.println($_);
    }}
Options : 1) error
2) $_
3)5.0
4)none of the above
33) public class Demo{
         public static void main(String[] arr){
                  Integer num1 = 100;
     Integer num2 = 100;
                  Integer num3 = 500;
                  Integer num4 = 500;
                      if(num1==num2){
                           System.out.println("num1 == num2");
                  }
                  else{
                           System.out.println("num1 != num2");
                  }
                  if(num3 == num4){
                           System.out.println("num3 == num4");
                  }
                  else{
                           System.out.println("num3 != num4");
                  }
  }
}
Ans ) num1==num2
      num3!=num4
34) public class Demo{
        public static void main(String[] arr){
                 }
        public static void main(String arr){
                 }
}
Option : 1) error 2) nothing
[hint: we can overload main() too ]
35)
// 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);
        }
}
Q) write the output and explain with appropriate reason?
 36)
class Test {
        public static void main(String[] args) {
                 for(int i = 0; 1; i++) {
                           System.out.println("Hello");
                           break;
                 }
        }
}
Q) give the output and the reason
37)
class Main {
        public static void main(String args[]) {
                 System.out.println(fun());
        }
        int fun() {
                 return 20;
        }
}
Q) FIND THE ERROR AND WRITE THE CORRECTED VERSION OF THE CODE ( OBJECT CREATION NOT
ALLOWED)
38) 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);
         }
}
Q) give the output and rectify if there is any error
[hint : there are no access issues coz of the protected class]
39)
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);
           }
}
Q) Predict the output
Ans ) 10
40)
public class Calculator
{
         int num = 100;
         public void calc(int num) { this.num = num * 10; }
         public void printNum() { System.out.println(num); }
           public static void main(String[] args)
           {
                    Calculator obj = new Calculator();
                    obj.calc(2);
                    obj.printNum();
           }
}
41) class Gfg
{
        // constructor
        Gfg()
        {
                System.out.println("Geeksforgeeks");
        }
           static Gfg a = new Gfg(); //line 8
           public static void main(String args[])
        {
                Gfg b; //line 12
                b = new Gfg();
        }
}
Ans) Geeksforgeeks
     Geeksforgeeks
We know that static variables are called when a class loads and static variables are called only once.
Now line 13 results to creation of object which inturn calls the constructor and “Geeksforgeeks” is
printed second time.
If in line 8 static variable would not have been used the object would have been called
42) public class Gfg
{
        public static void main(String[] args)
        {
                 Integer a = 128, b = 128;
                 System.out.println(a == b);
                Integer c = 100, d = 100;
                System.out.println(c == d);
        }
}
[hint : concept of Integer class range (-128 to 127)]
43) public class A
{
public static void main(String args[])
{
//\u000d System.out.println("hello");
}
}
Answer: b
Explanation: As we see the print statement it seems that the statement is commented but not so.
// \u000d is a new line Unicode character.
44) public class Test
{
public static void main(String args[])
{
int i=20+ +9- -12+ +4- -13+ +19;
System.out.println(i);
}
}
[Hint : look carefully in the calculation line. Are there unary or binary or normal operators?]
45) public class _C
{
private static int $;
public static void main(String main[])
{
String a_b = “ “; // this is normal quotes…ignore the typo
System.out.print($);
System.out.print(a_b);
}
}
Q) predict the output
46) public static void main(String args[])
{
String str="ONE"+1+2+"TWO"+"THREE"+3+4+"FOUR"+"FIVE"+5;
System.out.println(str);
}
    a.   ONE12TWOTHREE7FOURFIVE5
    b.   ONE3TWOTHREE34FOURFIVE5
    c.   ONETWOTHREEFOURFIVE15
    d.   ONE12TWOTHREE34FOURFIVE5
    e.   None of these
47) public class Demo
{
static int x=1111;
static
{
x=x-- - --x;
}
{
x=x++ + ++x;
}
public static void main(String args[])
{
System.out.println(x);
}
}
Ans ) 1110
48) Give the output of the following code : [2] (ICSE)
String P = “20”, Q = “19”,
int a = Integer .parselnt(P);
int b = Integer. valueOf(Q);
System.out.println(a+””+b);
49)
[ICSE]
 Give the output of following code and mention how many times the loop will execute ? [2]
int i;
for(i=5; i> =l;i~)
{
if(i%2 ==1)
continue;
System.out.print(i+ ”
}
50) What will be the output of the following code?
float x = 7.87;
 System.out.println(Math.ceil(x);
 System.out.println(Math.floor(x);
--------------------------------------------------------------------------------------------------------------------------------------