Curioustab
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Java.lang Class Questions
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 */
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);
In Java (IEEE 754 math), what does the following print? System.out.println(Math.sqrt(-4D));
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"); } } }
In Java (String immutability and concatenation), what is printed? String s = "ABC"; s.toLowerCase(); s += "def"; System.out.println(s);
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); } }
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"); }
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"); } }
1
2