0% found this document useful (0 votes)
17 views17 pages

OutPut Generation Set1

The document contains a series of Java code snippets and their expected outputs or behaviors based on given conditions and inputs. It covers various programming concepts such as data types, control structures, loops, and string manipulations. Each section provides specific examples and asks for predictions or explanations of the code's functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views17 pages

OutPut Generation Set1

The document contains a series of Java code snippets and their expected outputs or behaviors based on given conditions and inputs. It covers various programming concepts such as data types, control structures, loops, and string manipulations. Each section provides specific examples and asks for predictions or explanations of the code's functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Output Generation

Part 1
1>char x, y;
x = 'y';
System.out.println(x);
y = 'z';
System.out.println(y);
x = y;
System.out.println(x);

2>char ch = 'F';
int m = ch;
m=m+5;
System.out.println(m + " " + ch);

3>void test3(char c)
{
System.out.println( (int) c);
}
if 'm' is passed to c.

4>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 method.

5>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.

6>void test2(int a, int b)


{
while( a != b)
{
if ( a > b)
a = a — b;
else

Surajit Acharya [9831677218 WAPP/8240524380] Page 1


a = b — a;
}
System.out.println(a);
}
if 4 and 17 are passed to the function.

7>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( );
}

8>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( );
}

9>int a,b;
for (a=1; a<=2; a++)
{
for (b= (64+a); b<=70; b++)
System.out.print((char) b);
System.out.println( );
}

10>int i,j;
for (i=0; i<4; i++)
{
for (j=i; j>=0; j--)
System.out.print(j);
System.out.println();
}

11>int m=3,n=5,p=4;
if(m==n && n!=p)
{

Surajit Acharya [9831677218 WAPP/8240524380] Page 2


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);
}

12>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));
}

13>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);
}

14>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?

15>switch (opn)
{
case 'a':
System.out.println("Platform Independent");
break;
case 'b':

Surajit Acharya [9831677218 WAPP/8240524380] Page 3


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'

16>int a=10,b=12;
if(a==10&&b<=12)
a--;
else
++b;
System.out.println(a + "and" +b);

17>int a=10,b=12;
if(a>=10)
a++;
else
++b;
System.out.println(a + "and" +b);

18>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);

19>int x = 1,y = 1;
if(n > 0)
{
x = x + 1;
y = y + 1;
}
System.out.println(x + " , " + y);
What will be the values of x and y, if the value of n is given as:
(i) 1
(ii) 0 ?

20>int a=1,b=1,m=10,n=5;
if((a==1)&&(b==0))

Surajit Acharya [9831677218 WAPP/8240524380] Page 4


{
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));
}

21>System.out.println("nine:" + 5 + 4);
System.out.println("nine:" + (5 + 4));

22>int m[] = {2,4,6,8};


System.out.println(m[1] + " " + m[2]);

23>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);

24>int a[4]={2,4,6,8};
for(i=0;i<=1;i++)
{
s=a[i]+a[3-i];
System.out.println(s);
}

25>
int a[]=new int [5];
a[0]=4; a[1]=8; a[2]=7; a[3]=12; a[4]=3;
System.out.println(a[2+1]);

26>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);

27>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);

28>System.out.println(Math.floor(-4.7));
System.out.println(Math.max(Math.ceil(14.55),15.5));

Surajit Acharya [9831677218 WAPP/8240524380] Page 5


System.out.println(Math.rint(-9.4)+Math.sqrt(9.0));
System.out.println(Math.ceil(3.4)+Math.pow(2,3));
System.out.println(Math.sqrt(Math.min(42.5,42.25)));
System.out.println(Math.rint(-99.4));
System.out.println(Math.rint(98.5));
System.out.println(Math.floor(-0.88));
System.out.println(Math.max(-77.66, -87.45));
System.out.println(Math.min(-25.5, -12.5));
System.out.println(Math.ceil(65.5));
System.out.println(Math.round(-18.51));
System.out.println(Math.ceil(-0.95));

29>String P = "20", Q ="19";


int a = Integer.parseInt(P);
int b = Integer.valueOf(Q);
System.out.println(a+""+b);

30>char c = 'B';
int i = 4;
System.out.println(c+i);
System.out.println((int)c+i);

31>String s= "7";
int t =Integer.parseInt(s);
t=t+1000;
System.out.println(t);

32>char c = 'B';
int i = 4;
System.out.println(c+i);
System.out.println((int)c+i);

33>char ch = 'x'; int n = 5;


n = n + (int)ch;
char c = (char)n;
System.out.println((char)((int)c-26));

34>int n = 97;
char ch = Character.toUpperCase((char)n);
System.out.println(ch + " Great Victory");

35>String s= "7";
int t =Integer.parseInt(s);
t=t+1000;
System.out.println(t);

36>char c = 'A';
int n = (int) c + 32;

Surajit Acharya [9831677218 WAPP/8240524380] Page 6


System.out.println((char)n);

37>char ch = 'A';
char chr = Character.toLowerCase(ch);
int n = (int)chr-32;
System.out.println((char)n + "\t" + chr);

38>char ch = 'y';
char chr = Character.toUpperCase(ch);
int p = (int) chr;
System.out.println(chr + "\t" + p);

39>char ch = '*';
boolean b = Character.isLetter(ch);
System.out.println(b);

40>String A ="26", B="100";


String D=A+B+"200";
int x= Integer.parseInt(A);
int y = Integer.parseInt(B);
int d = x+y;
System.out.println("Result 1 = " + D);
System.out.println("Result 2 = " + d);

41>String a="10", b="20";


int x=Integer.parseInt(a);
int y=Integer.valueOf(b);
System.out.println(x+y);
System.out.println(a+b);

42>String s1 = "phoenix"; String s2 ="island";


System.out.println(s1.substring(0).concat(s2.substring(2)));
System.out.println(s2.toUpperCase());

43>String x = "Vision";
String y = "2020";
System.out.println(x.charAt(3));

44>String x = "Computer";
String y = "Applications";
System.out.println(x.equals(y));

45>String x = "Computer";
String y = "Applications";System.out.println(x.substring(1,5));

46>String S1 = "Computer World";


String S2 = "COMPUTER WORLD";
String S3 = "Computer world";

Surajit Acharya [9831677218 WAPP/8240524380] Page 7


String S4 = "computer world";
System.out.println(S1 + " equals "+ S2 + " " + S1.equals(S2));
System.out.println(S1 + " equals "+ S3 + " " + S1.equals(S3));
System.out.println(S1 + " equals "+ S4 + " " + S1.equals(S4));
System.out.println(S1 + " equalsIgnoreCase "+ S4 + " " + S1.equalsIgnoreCase(S4));

47>String str = "Information Technology";


int p;
p = str.indexOf('n');
System.out.println(p);

48>String x = "Computer";
String y = "Applications";System.out.println(x.indexOf(x.charAt(4)));

49>boolean p;
p = ("BLUEJ".length() > "bluej".length()) ? true: false;

50>String x = "Computer";
String y = "Applications";
System.out.println(y + x.substring(5));

51>String str1 = "Information Technology";


String str2 = "information technology";
boolean p = str1.equalsIgnoreCase(str2);
System.out.println("The result is " + p);

52>String n = "Computer Knowledge";


String m = "Computer Applications";
System.out.println(n.substring(0,8).concat(m.substring(9)));
System.out.println(n.endsWith("e"));

53>String x = "Vision";
String y = "2020";
System.out.println(x.equals(y));

54>String x[] = {"SAMSUNG", "NOKIA", "SONY", "MICROMAX", "BLACKBERRY"};


System.out.println(x[3].length());

55>String arr[]= {"DELHI", "CHENNAI", "MUMBAI", "LUCKNOW", "JAIPUR"};


System.out.println(arr[0].length() > arr[3].length());
System.out.print(arr[4].substring(0,3));

56>String n1 = "46", n2 = "64";


int total = Integer.parseInt(n1) + Integer.parseInt(n2);
System.out.println("The sum of " + "46 " + "and" + " 64" + " is " + total);

57>String str = "Computer Applications" + 1 + 0;


System.out.println("Understanding" + str);

Surajit Acharya [9831677218 WAPP/8240524380] Page 8


58>String s="Today is Test";
System.out.println(s.indexOf('T'));
System.out.println(s.substring(0,7) + " " +"Holiday");

59>String a ="Smartphone", b="Graphic Art";


String h=a.substring(2, 5);
String k=b.substring(8).toUpperCase();
System.out.println(h);
System.out.println(k.equalsIgnoreCase(h));

60>String s1 = "Life is Beautiful";


System.out.println("Earth" + s1.substring(4));
System.out.println(s1.endsWith("L"));

61>void strop(String s) {
char a = s.charAt(3);
int b = s.indexOf('M');
String t = s.substring(3,6);
boolean p = s.equals(t);
System.out.println(a + " " + b + " " + t + " " + p);
}
What will be the output for strop("COMPUTER")?

62>int v,s,n=550;
s = n + v > 1750? 400:200;
When,

(a)v = 500 (b)v = 1500

63>If a = 0, b = 30, c = 40; then find the value of 'a' when:


a += --b + c++ + b;

64>Evaluate the following expressions if the values of the variables are a = 2, b = 3, and c = 9.

(a) a - (b++) * (--c);


(b) a * (++b) % c;

65>What will be the output of the following if x = 5 initially?

(a) 5* ++x;
(b) 5* x++;

66>If a = 5, b = 9, calculate the value of a in the following expression:


a += a++ - ++b + a

67>If m = 5 and n = 2, predict the output values of m and n:

Surajit Acharya [9831677218 WAPP/8240524380] Page 9


(a) m -= n;
(b) n = m + m/n;

68>int a=0,b=10,c=40;
a = --b + c++ + b;
System.out.println(" a = " + a);

69>Rewrite the following program segment using if-else statements instead of the ternary operator:
String grade = (marks>=90)?"A": (marks>=80)? "B": "C";

70>Rewrite the following program segment using if-else statements instead of the ternary operator:
net = (salary > 10000) ? salary - (8.33/100)*salary : salary - (5/100)*salary

71>int a = 14, b = 4;
boolean x = (a > b) ? true : false;

72>If int y =10 then find int z = (++y * (y++ + 5));

73>if(a > b)
{
if (a > c)
g = a;
else
g = c;
}
else if (b > c)
g = b;
else
g = c;

74>int a = 18; int b = 12;


boolean t = (a > 20 && b < 15)? true : false;

75>Rewrite the following using ternary operator:

if (bill > 10000)


discount=bill*10.0/100;
else
discount=bill*5.0/100;

76>Rewrite the following program segment using if-else statements instead of the ternary operator:

c = (x >= 'A' && x<= 'Z') ? "Upper Case Letter" : "Lower Case Letter";

77>Predict the output of the below Java program snippet:

c = (val + 550 < 1700)? 200: 400;


if: (a) val = 1000 (b) val = 1500

Surajit Acharya [9831677218 WAPP/8240524380] Page 10


78>Give the output of the following expression:
a+= a++ + ++a + --a + a--; when a = 7;

79>What is the value of y after the execution?


y+= ++y + y-- + --y; when int y=8

80>Rewrite the following program segment using if-else statements instead of the ternary operator:

s = (a + b < c || a + c <= b || b + c <= a) ? "Triangle is not possible": "Triangle is possible";

81>int k=5,j=9;
k+= k++ - ++j + k;
System.out.println("k="+k);
System.out.println("j="+j);

82>Determine the output of the following program.

public class PredictOutput2


{
public static void main(String args[])
{
int a = 6, b = 2, c = 3;
System.out.println("Output 1: " + (a == b * c));
System.out.println("Output 2: " + (a == (b * c)));
}
}

83>Determine the output of the following program.

public class PredictOutput3


{
public static void main(String args[])
{
int a = 2, b = 2, c = 2;
System.out.println("Output 1: " + (a + 2 < b * c));
System.out.println("Output 2: " + (a + 2 < (b * c)));
}
}

84>Determine the output of the following program.

public class Test


{
public static void main(String[] args)
{
int a = 1, b = 2;
System.out.println("Output1: " + a + b);

Surajit Acharya [9831677218 WAPP/8240524380] Page 11


System.out.println("Output2: " + (a + b));
}
}

85>Determine the output of the following program.

public class PredictOutput1


{
public static void main(String args[])
{
int a = 4, b = 2, c = 3;
System.out.println("Output 1: " + (a = b * c));
System.out.println("Output 2: " + (a = (b * c)));
}
}

86>int a=0,b=10,c=40;
a = --b + c++ +b;
System.out.println(" a = " + a);

87>int a = 10, b =12;


if(a>=10)
a++;
else
++b;
System.out.println(" a = " + a + " and b = " +b);

88>int a=6,b=5;
a += a++ % b++ *a + b++* --b;

89>int a=6,b=5,c;
c = (a++ % b++) *a + ++a*b++;

90>Give the output of following code and mention how many times the loop will execute?

int i;
for( i=5; i>=1; i--)
{
if(i%2 == 1)
continue;
System.out.print(i+" ");
}

91>State the final value of q at the end of the following program segment after execution. Show the dry
run.

for(m=2;m<=3;++m)
{

Surajit Acharya [9831677218 WAPP/8240524380] Page 12


for(n=1;n<=m;++n)
{
p=m+n-1;
if(p%3 == 0)
q += p;
else
q += p+4;
}
}

92>int m=2;
int n=15;
for(int i=1;i<5;i++)
m++;
--n;
System.out.println("m="+m);
System.out.println("n="+n);

93>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

94>int a,b;
for(a=6;b=4; a <= 4; a=a+ 6)
{
if(a%b==0)
break;
}
System.out.println(a);

95>int k=1,i=2;
while(++i<6)
k*=i;
System.out.println(k);

96>int i;
for(i = 5; i > 10; i++)
System.out.println(i);
System.out.println(i * 4);

97>x = 5; y = 50;
while(x<=y)

Surajit Acharya [9831677218 WAPP/8240524380] Page 13


{
y = y / x;
System.out.println(y);
}

98>Determine how many times the body of the loop will be executed and predict the output.

class dk4
{
public static void main(String args[])
{
int x=5,y=50;
while(x<=y)
{
y=y/x;
System.out.println(y);
}
}
}

99>class dk2
{
public static void main(String args[])
{
int i=2,k=1;
while (++i<6)
k *= i;
System.out.println(k);
}
}

100>class dk3
{
public static void main(String args[])
{
int m=2,n=15;
for(int i=1;i<=5;i++)
{
m++;--n;
System.out.println("m="+m);
System.out.println("n="+n);
}
}
}

101>for(i = -1;i<10;i++)
{
System.out.println(++i);

Surajit Acharya [9831677218 WAPP/8240524380] Page 14


}

102>int p = 9;
while (p<=15)
{
p++;
if(p== 10)
continue;
System.out.println(p);
}

103>int a = 3;
while (a<=10)
{
a++;
if(a== 5)
continue;
System.out.println(a);
}

104>int a,b;
for (a = 6, b = 4; a <= 24; a = a + 6)
{
if (a%b == 0)
break;
}
System.out.println(a);

105>char ch ;
int x=97;
do
{
ch=(char)x;
System.out.print(ch + " " );
if(x%10 == 0)
break;
++x;
} while(x<=100);

106>int k;
for ( k = 5 ; k < = 20 ; k + = 7 )
if ( k% 6==0 )
continue;
System.out.println(k);

107>int x, y;
for (x = 9, y = 4; x <= 45; x+=9) {

Surajit Acharya [9831677218 WAPP/8240524380] Page 15


if (x % y == 0)
break;
}
System.out.println(x);

108>int x=4;
x += (x++) + (++x) + x;

109>double x = 2.9, y = 2.5;


System.out.println(Math.min(Math.floor(x), y));
System.out.println(Math.max(Math.ceil(x), y));

110>String s = "Examination";
int n = s.length();
System.out.println(s.startsWith(s.substring(5, n)));
System.out.println(s.charAt(2) == s.charAt(6));

111>String s1 = "Computer", s2 = "Applications";


a = (s1.compareTo(s2));
b = (s1.equals(s2));

112>String s = "malayalam";
System.out.println(s.indexOf('m'));
System.out.println(s.lastIndexOf('m'));

113>If int n[] ={1, 2, 3, 5, 7, 9, 13, 16} what are the values of x and y?
x=Math.pow(n[4],n[2]);
y=Math.sqrt(n[5]+[7]);

114>What is the final value of ctr after the iteration process given below, executes?

int ctr=0;
for(int i=1;i&lt;=5;i++)
for(int j=1;j&lt;=5;j+=2)
++ctr;

115>String n = “Computer Knowledge”;


String m = “Computer Applications”;
System.out.println(n.substring (0,8). concat (m.substring(9)));
System.out.println(n.endsWith(“e”));

116>int k = 5, j = 9;
k += k++ – ++j + k;
System.out.println("k= " +k);
System.out.println("j= " +j);

117>int m=2;
int n=15;

Surajit Acharya [9831677218 WAPP/8240524380] Page 16


for(int i = 1; i<5; i++);
m++; –-n;
System.out.println("m=" +m);
System.out.println("n="+n);

118>char x = 'A' ; int m;


m=(x=='a') ? 'A' : ‘a’;
System.out.println("m="+m);

119>int k=1, i=2;


while (++i<6)
k*=i;
System.out.println(k);

120>int a = 9; a++;
System.out.println (a);
a -= a – - – a;
System .out.println (a);

121>long num=729, sum 0;


for( long y= num; y> 0; y= y/10){
sum= sum + y % 10;
}
System.out.println("Sum of digits = "+ sum);

122>If int y = 10 then find int z = (++y * (y++ + 5));

123>System.out.print(“My Friends are \n”);


System.out.print(“Ronit \tNihir \t”);
System.out.print(“and Ananya too”);

124>char ch[ ]= {‘I’, ‘N’, T’, E’, ‘L’, P’, ‘E’, ‘N’, ‘T’, ‘I’, ‘U’, ‘M’};
String obj= new String(ch, 3, 4);
System.out.println(“The result is = “+ obj);

125>String str1 = ”great”;


String str2= “Minds”;
System.out.println(str1.substring(0,2). Concat(str2.substring(1)));
System.out.println(( “WH”+(str1.substring(2).toUpperCase() )));

126>int a= new int(5);


for(int i=0; i<=5; i++)
a[i]= I;

Surajit Acharya [9831677218 WAPP/8240524380] Page 17

You might also like