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
Java inheritance and constructors: given A has only A(int) and B extends A with no explicit constructor, what happens when creating new B()? class A { public A(int x){} } class B extends A {} public class test { public static void main(String[] args) { A a = new B(); System.out.println("complete"); } }
Java pass-by-value with String vs StringBuffer: what concatenated output is printed? public class Test138 { public static void stringReplace(String text) { text = text.replace('j', 'c'); } public static void bufferReplace(StringBuffer text) { text = text.append("c"); } public static void main(String[] args) { String textString = new String("java"); StringBuffer textBuffer = new StringBuffer("java"); stringReplace(textString); bufferReplace(textBuffer); System.out.println(textString + textBuffer); } }
Java equals symmetry between String and Object references to the same string: what prints? public class Test178 { public static void main(String[] args) { String s = "foo"; Object o = (Object) s; if (s.equals(o)) { System.out.print("AAA"); } else { System.out.print("BBB"); } if (o.equals(s)) { System.out.print("CCC"); } else { System.out.print("DDD"); } } }
Java String allocation count: given String x = new String("xyz"); String y = "abc"; x = x + y; how many distinct String objects are created?
Java short-circuiting, do-while, and side effects: what value of j prints? public class ExamQuestion7 { static int j; static void methodA(int i){ boolean b; do { b = i < 10 | methodB(4); b = i < 10 || methodB(8); } while(!b); } static boolean methodB(int i){ j += i; return true; } public static void main(String[] args){ methodA(0); System.out.println("j = " + j); } }
Java threading and synchronization ordering: given two anonymous threads appending to StringBuffers with cross locks, what can be said about the output order? public class Test { public static void main(String[] args) { final StringBuffer a = new StringBuffer(); final StringBuffer b = new StringBuffer(); new Thread(){ public void run(){ System.out.print(a.append("A")); synchronized(b){ System.out.print(b.append("B")); } } }.start(); new Thread(){ public void run(){ System.out.print(b.append("C")); synchronized(a){ System.out.print(a.append("D")); } } }.start(); } }
In Java, what is the exact output of this String-manipulation program? Ensure you trace substring indexes and character concatenation step-by-step. String a = 'newspaper'; a = a.substring(5, 7); char b = a.charAt(1); a = a + b; System.out.println(a);
Java Boolean wrappers: evaluate == versus equals when constructed from case-varied strings, and compute the final result value. public class BoolTest { public static void main(String [] args) { int result = 0; Boolean b1 = new Boolean("TRUE"); Boolean b2 = new Boolean("true"); Boolean b3 = new Boolean("tRuE"); Boolean b4 = new Boolean("false"); if (b1 == b2) result = 1; // Line 10 if (b1.equals(b2)) result = result + 10; // Line 12 if (b2 == b4) result = result + 100; // Line 14 if (b2.equals(b4)) result = result + 1000; // Line 16 if (b2.equals(b3)) result = result + 10000; // Line 18 System.out.println("result = " + result); } }
Java Strings are immutable: after toLowerCase and unused replace calls, what exactly is printed? String a = 'ABCD'; String b = a.toLowerCase(); b.replace('a', 'd'); b.replace('b', 'c'); System.out.println(b);
Java Math.sqrt with a negative argument: what will this program print? public class SqrtExample { public static void main(String [] args) { double value = -9.0; System.out.println(Math.sqrt(value)); } }
Java interfaces: accessing a constant field declared in an interface via object, class, and interface names — does this compile and run? interface Foo141 { int k = 0; // implicitly public static final } public class Test141 implements Foo141 { public static void main(String args[]) { int i; Test141 test141 = new Test141(); i = test141.k; // Line 11 i = Test141.k; // inherited constant i = Foo141.k; // interface-qualified } }
Java mixed numeric + String concatenation: evaluate left-to-right and determine the printed result. class Q207 { public static void main(String[] args) { int i1 = 5; int i2 = 6; String s1 = "7"; System.out.println(i1 + i2 + s1); // Line 8 } }
Java Object vs String equality symmetry: given the outputs A/B/C/D, which two are printed? String s = 'hello'; Object o = s; if (o.equals(s)) { System.out.println("A"); } else { System.out.println("B"); } if (s.equals(o)) { System.out.println("C"); } else { System.out.println("D"); } // Which labels are printed among A B C D?
Java reference equality and equals on the same object: compute the final result value printed. public class ObjComp { public static void main(String [] args ) { int result = 0; ObjComp oc = new ObjComp(); Object o = oc; if (o == oc) result = 1; if (o != oc) result = result + 10; if (o.equals(oc)) result = result + 100; if (oc.equals(o)) result = result + 1000; System.out.println("result = " + result); } }
Java rounding functions: count how many elements satisfy Math.round(x + 0.5) == Math.ceil(x). public class Example { public static void main(String [] args) { double values[] = {-2.3, -1.0, 0.25, 4}; int cnt = 0; for (int x = 0; x < values.length; x++) { if (Math.round(values[x] + .5) == Math.ceil(values[x])) { ++cnt; } } System.out.println("same results " + cnt + " time(s)"); } }
Java instanceof evaluation order: with Tree tree = new Pine(), which branch executes and what is printed? class Tree {} class Pine extends Tree {} class Oak extends Tree {} public class Forest1 { public static void main (String [] args) { Tree tree = new Pine(); if (tree instanceof Pine) System.out.println("Pine"); else if (tree instanceof Tree) System.out.println("Tree"); else if (tree instanceof Oak) System.out.println("Oak"); else System.out.println("Oops"); } }
In Java (wrapper equality and autoboxing nuances), what is the final printed value of result for the following program? public class WrapTest { public static void main(String [] args) { int result = 0; short s = 42; Long x = new Long("42"); Long y = new Long(42); Short z = new Short("42"); Short x2 = new Short(s); Integer y2 = new Integer("42"); Integer z2 = new Integer(42); if (x == y) /* Line 13 */ result = 1; if (x.equals(y) ) /* Line 15 */ result = result + 10; if (x.equals(z) ) /* Line 17 */ result = result + 100; if (x.equals(x2) ) /* Line 19 */ result = result + 1000; if (x.equals(z2) ) /* Line 21 */ result = result + 10000; System.out.println("result = " + result); } }
In Java Strings (immutability and case conversion), what is printed by the following code? String x = "xyz"; x.toUpperCase(); /* Line 2 */ String y = x.replace('Y', 'y'); y = y + "abc"; System.out.println(y);
In Java (String references and reassignment), what does this program print? public class StringRef { public static void main(String [] args) { String s1 = "abc"; String s2 = "def"; String s3 = s2; /* Line 7 */ s2 = "ghi"; System.out.println(s1 + s2 + s3); } }
In Java (Math.random casting), what value is assigned to i in the statement below? int i = (int) Math.random();
1
2