0% found this document useful (0 votes)
33 views6 pages

Java Midterm Test Code Snippets

The document contains a Java mid-term test with various code snippets that require students to determine the output of each snippet. It covers topics such as variable manipulation, string methods, and the Math class functions. Additionally, it includes questions about compilation errors and the behavior of specific Java operations.

Uploaded by

ishmitsinghania
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)
33 views6 pages

Java Midterm Test Code Snippets

The document contains a Java mid-term test with various code snippets that require students to determine the output of each snippet. It covers topics such as variable manipulation, string methods, and the Math class functions. Additionally, it includes questions about compilation errors and the behavior of specific Java operations.

Uploaded by

ishmitsinghania
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

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

Common questions

Powered by AI

Using replaceFirst(), only the first occurrence of 'o' will be replaced, resulting in 'bleod'. Using replaceAll() and replace(), all occurrences of 'o' will be replaced, resulting in 'bleed'. These methods demonstrate different behaviors of replacing substrings in Java .

The expression '4++' tries to apply the increment operator to a constant, which is illegal. Increment operators must be applied to a variable, causing a compilation error .

ceil() returns the smallest integer greater than or equal to the argument while floor() returns the largest integer less than or equal to the argument. They are opposites in rounding policies; ceil rounds up, floor rounds down .

The operation uses 's.substring(0,1).toUpperCase().concat(s.substring(1).toLowerCase())', resulting in 'Jackson'. This approach efficiently utilizes substring and concat methods with case conversion .

The output will be 'heLLo' as the replace method will substitute all occurrences of 'l' with 'L'. This replacement maintains the structure but changes the specified character .

The expression "++p%--q + ++r/--q;" will be evaluated as follows: p is incremented first to 4, q is decremented to 3, and r is incremented to 6. The modulo operation 4%3 results in 1, and the division 6/3 results in 2. Adding these together gives s = 3 .

The method extracts characters by checking if they match their uppercase representation. The resultant string is 'MESS' as it only concatenates uppercase letters .

The output is "<Good Morning!>". Concatenation is called sequentially on each string, joining with a space and appending an exclamation mark, resulting in the specified sequence .

The code will not compile because the expression "i * i + j" results in an int, and you cannot implicitly cast an int to a byte without explicit casting in Java .

An error will occur because split() works with regular expressions and requires strings for the delimiter. The correct approach should involve indices with substring or using a string pattern. Therefore, an exception results if using char directly .

You might also like