Java Output Based Questions
Java Output Based Questions
Java Output Based Questions
By S. Mukherjee
[**Questions have been taken from various sources and the answers provided are mostly correct.)
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.
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( );
}
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);
29) System.out.println(Math.max(Math.ceil(14.55),15.5));
Ans ) num1==num2
num3!=num4
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);
}
}
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)
// filename Main.java
class Point {
protected int x, y;
39)
class Test {
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); }
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
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?]
a. ONE12TWOTHREE7FOURFIVE5
b. ONE3TWOTHREE34FOURFIVE5
c. ONETWOTHREEFOURFIVE15
d. ONE12TWOTHREE34FOURFIVE5
e. None of these
Ans ) 1110
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+ ”
}
--------------------------------------------------------------------------------------------------------------------------------------