1
JAVA MID TERM TEST-1
1. What is the output of the following code snippet?
int a, b, x = 5;
a = ++x;
b = x++;
[Link]("a: " + a);
[Link]("b: " + b);
[Link]("x: " + x);
2. What is the output of the following code snippet?
int a, b, x = 5;
a = x++;
b = ++x;
[Link]("a: " + a);
[Link]("b: " + b);
[Link]("x: " + x);
3. What is the output of the following code snippet?
int a, x = 5;
a = x++ + ++x;
[Link]("a: " + a);
[Link]("x: " + x);
4. What is the output of the following code snippet?
int a, b, x = 5, y = 4;
a = x++ + ++y;
b = ++x + --y;
[Link]("a: " + a);
[Link]("b: " + b);
5. What is the output of the following code snippet?
int a, x = 12, y = 6;
a = x++ % ++y;
[Link]("a: " + a);
6. What is the output of the following code snippet?
int a = 3, b = 4;
int x = a++*++b+a++*++b;
[Link]("x: " + x);
2
JAVA MID TERM TEST-1
7. What is the output of the following code snippet?
int a = 3, b = 4, c=5;
int x = c*a>b?a:b;
[Link]("x: " + x);
8. What is the output of the following code snippet?
int p = 3, q = 4, r=5, s=5;
r += p>q?p:q; // (1)
s = s+p>q?p:q; // (2)
[Link]("r: " + r);
[Link]("s: " + s);
9. What is the output of the following code snippet?
int p = 3, q = 4, r=5, s;
s = ++p%--q + ++r/--q;
[Link]("s: " + s);
10. What is the output of the following code snippet?
int p = 15, q = 12, s;
s = p/q*++p%(++p%--q);
[Link]("s: " + s);
11. What is the output of the following code snippet?
[Link](4++);
12. What is the output of the following code snippet?
char ch = 'A';
[Link](ch);
ch++;
[Link](ch);
++ch;
[Link](ch);
13. What is the output of the following code snippet?
char ch = 'D';
ch += 6;
3
JAVA MID TERM TEST-1
[Link]("ch: " + ch);
14. What is the output of the following code snippet?
char ch = 'D';
ch = ch + 6;
[Link]("ch: " + ch);
15. What is the output of the following code snippet?
char ch = 'D';
ch = (char)(ch + 6);
[Link]("ch: " + ch);
16. . What is the output of the following code snippet?
for(i = 65; i <= 70; i++)
{
ch = (char)i;
[Link](ch);
}
[Link]();
17. What is the output of the following code snippet?
String s = "hello";
for(int i=0; i < [Link](); i++)
{
char ch = [Link](i);
char chNew = (char)(ch + i + 1);
[Link]( chNew );
}
18. Will the following code snippet compile? Why?
int i = 4;
byte j = 2;
byte b = i * i + j;
[Link](b);
19. Will the following code snippet compile? Why?
byte b = 4;
byte p;
4
JAVA MID TERM TEST-1
p = ++b;
[Link](p);
20. Will the following code snippet compile? If no, why not? If yes, what is the output?
byte b = 1;
for(int i=1; i<= 127; i++)
b++;
[Link](b);
21. What is the output of the following code snippet?
String s = "hello";
[Link]( "1. " + [Link]() );
[Link]( "2. " + [Link](0) );
22. . What is the return type of the following methods of the String class?
a. charAt()
b. length()
c. equals()
d. lastIndexOf()
e. compareTo()
f. concat()
g. replace()
23. What is the output of the following code snippet?
String s1 = "hello", s2 = "Hello", s3 = "hello";
[Link]( "1. " + [Link](s2) );
[Link]( "2. " + [Link](s3) );
[Link]( "3. " + [Link](s2) );
[Link]( "4. " + [Link](s2) );
[Link]( "5. " + [Link](s1) );
[Link]( "6. " + [Link](s1) );
24. What is the output of the following code snippet?
[Link]( "hell".compareTo("hello") );
[Link]( "hello".compareTo("hell") );
5
JAVA MID TERM TEST-1
25. What is the output of the following code snippet?
String s1 = "Good";
String s2 = " "; // single space
String s3 = "Morning";
String s = [Link](s2).concat(s3).concat("!");
[Link]( "Output: <" + s + ">" );
26. What is the output of the following code snippet?
String s1 = "blood"; // (1)
String s2 = [Link]("o", "e"); // (2)
String s3 = [Link]("o", "e"); // (3)
String s4 = [Link]("o", "e"); // (4)
String s5 = [Link]('o', 'e'); // (5)
[Link]( "s1: " + s1 ); // (1)
[Link]( "s2: " + s2 ); // (2)
[Link]( "s3: " + s3 ); // (3)
[Link]( "s4: " + s4 ); // (4)
[Link]( "s5: " + s5 ); // (5)
27. What is the output of the following code snippet?
String s = "Mississipi";
[Link]( [Link]("is") );
[Link]( [Link]("is") );
[Link]( [Link]("Miss") );
[Link]( [Link]("ss", "dd") );
[Link]( [Link]("ss", "dd") );
[Link]( [Link]("i", "e") );
[Link]( [Link]('i', 'e') );
[Link]( [Link](4) );
28. What is the output of the following code snippet?
String s1 = "Good Morning";
String s2 = "Sweet Dreams";
String s = [Link](0, [Link]('M')).concat( [Link]( [Link]('D') ) );
[Link](s);
29. What is the output of the following code snippet?
String sentence = "MoreEnergySavingSchemes";
String s = "";
int i;
for(i=0; i < [Link](); i++)
{
String temp = [Link](i,i+1);
6
JAVA MID TERM TEST-1
if( [Link]( [Link]() ) )
s = [Link](temp);
}
[Link]("Answer: " + s);
30. Using String functions in a SINGLE STATEMENT, convert the variable s = “jaCKsOn” such
that the first letter is in uppercase and the rest are all in lowercase. Use the substring() and
concat() functions.
31. Which package does the Math class belong to?
32. What is the fully qualified name of the Math class in Java?
33. Give the name of the Math class function that performs the following:
a. Returns the absolute value of the argument passed
b. Returns the largest non-fractional value less than or equal to the argument passed
c. Returns the smallest non-fractional value greater than or equal to the argument passed
d. Returns the greater of two arguments
e. Returns the lower of two arguments
f. Returns the natural log of the passed argument
g. Returns the log to base 10 of the passed argument
h. Returns the power of argument 1 raised to argument 2
i. Returns the closest non-fractional number to the passed argument
j. Returns a random number >= 0.0 and < 1
k. Returns the square root of the passed argument
l. Returns a “double” value that is closest in value to the argument passed, and is non-fractional.
34. Will the following code snippet compile? If no, why not? If yes, what is the output?
double d = Math.E;
int D = [Link](d);
[Link]("d: " + d);
[Link]("D: " + D);
35. Will the following code snippet compile? If no, why not? If yes, what is the output?
double d1 = 2.3, d2 = 4.5, d;
int i1 = 4, i2 = 2, i;
d = [Link]( d1, d2 );
i = [Link]( i1, i2 );
[Link]("d: " + d);
[Link]("i: " + i);