Java.lang Class Questions
Practice Java.lang Class MCQs with answers and explanations. Page 2 of 2.
Category
Java Programming
Topic
Java.lang Class
Page
2 / 2
Mode
Practice
Questions
Open any question to view the answer and explanation.
In Java (control flow with i++ and --j inside a do-while), what are the final values printed?
int i = 1, j = 10;
do
{
if(i++ > --j) /* Line 4 /
{
continue;
}
} while (i < 5);
System.out.println("i = " + i + " and j = " + j); / Line 9 */
Open
View answer
In Java (String vs StringBuilder API), what is the output or error?
String d = "bookkeeper";
d.substring(1, 7);
d = "w" + d;
d.append("woo"); /* Line 4 */
System.out.println(d);
Open
View answer
In Java (IEEE 754 math), what does the following print?
System.out.println(Math.sqrt(-4D));
Open
View answer
In Java (parsing and rounding), what does this code print?
public class NFE
{
public static void main(String [] args)
{
String s = "42";
try
{
s = s.concat(".5"); /* Line 8 */
double d = Double.parseDouble(s);
s = Double.toString(d);
int x = (int) Math.ceil(Double.valueOf(s).doubleValue());
System.out.println(x);
}
catch (NumberFormatException e)
{
System.out.println("bad number");
}
}
}
Open
View answer
In Java (String immutability and concatenation), what is printed?
String s = "ABC";
s.toLowerCase();
s += "def";
System.out.println(s);
Open
View answer
In Java (operators, short-circuiting, and identifiers), what is the output of this program or what error occurs?
public class ExamQuestion6
{
static int x;
boolean catch()
{
x++;
return true;
}
public static void main(String[] args)
{
x = 0;
if ((catch() | catch()) || catch())
x++;
System.out.println(x);
}
}
Open
View answer
In Java, wrapper conversions and numeric promotions: predict the output printed by this try-catch code.
try {
Float f1 = new Float("3.0");
int x = f1.intValue();
byte b = f1.byteValue();
double d = f1.doubleValue();
System.out.println(x + b + d);
} catch (NumberFormatException e) {
System.out.println("bad number");
}
Open
View answer
Java booleans, wrapper Boolean, and the single ampersand operator: determine the output (JDK 1.6+).
public class BoolTest {
public static void main(String[] args) {
Boolean b1 = new Boolean("false");
boolean b2;
b2 = b1.booleanValue();
if (!b2) {
b2 = true;
System.out.print("x ");
}
if (b1 & b2) { // single &: evaluates both sides
System.out.print("y ");
}
System.out.println("z");
}
}
Open
View answer
Practice smarter
Solve a few questions daily and revisit weak topics regularly to improve accuracy.