Some practice questions related to Core Java with answers .
Core Java Important Notes for interview preparationFull description
Full description
Full description
Full description
Hand Written Note by Natraj Sir
Core java objective questionsFull description
series 7
core cutter test
JAVADescripción completa
Descrição completa
Core Java interviewFull description
Core Java Practice Test
1. Which of the following statements are true? Select 2 options 1)
The The ide idea a of of thr threa eadi ding ng is the the sam same e as as mul multi ti task taskin ing. g.
2)
No two two thre thread ads s can can acce access ss the the same same vari variab able les s
3) runtime.
usin using g sync synchr hron oniz izat atio ion n with within in cod code e may may caus cause e a perf perfor orma manc nce e pena penalt lty y at
4) The The use use of thre thread ads s wit withi hin n a prog progra ram m may may make make the the exa exact ct path path of execution unpredictable answer : 1 & 2 2. What will happen when you attempt to compile and run the following code? public class TadKebab{ public static void main(String argv[]){ new TadKebab(); } private TadKebab(){ String[] Sa = {"Sam Smith", "John Smith"}; getName(Sa); System.out.print(Sa[0]); } public void getName(String s[]){ s[0]="Coors"; } } Select 1 option 1)
Comp Compil ile e tim time e err error or,, a cons constr truc ucto torr can canno nott be be mar marke ked d as as pri priva vate te
2)
Compil mpila ation tion and outpu tput of "Sa "Sam Smith mith""
3)
Compilation an and ou output of of "C "Coors"
4)
Compilation, but runtime error.
Answer : 3
3. Given the code. What is the result? public class TrickyNum { private X x; public TrickyNum(X x) { this.x = x; } private double getDouble() { return ((Double) x).doubleValue(); } public static void main(String args[]) { TrickyNum a = new TrickyNum(new Integer(1)); System.out.print(a.getDouble()); } } A) Compilation fails. B) An exception is thrown at runtime. C) "1.0" is printed. D) "1" is printed. Answer : B 4. Given the code. What is the result? import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.List; public class TryMe { public static void main(String args[]) { List list = new LinkedList(); list.add("one"); list.add("two"); list.add("three"); Collections.reverse(list); Iterator iter = list.iterator(); for (Object o : iter) { System.out.print(o + " ");
} } } A) "three two one " is printed B) "one two three " is printed C) Nothing is printed D) Compilation fails E) An exception is thrown at runtime Answer : D 5. What is true? (Choose three) A) A method with the same signature as a private final method in class Z can be implemented in a subclass of Z. B) A final method in class Z can be abstract if and only if Z is abstract. C) A protected method in class Z can be overriden by any subclass of Z. D) A private static method can be called only within other static methods in class Z. E) A non-static public final method in class Z can be overriden in any subclass of Z. F) A public static method in class Z can be called by a subclass of Z without explicitly referencing the class Z. Answer A,C,D 6. Given the code. Which statements are true? (Select two) public class Hotel { public static void book() { //some code goes here }
public void cancelBooking() { //some code goes here } } A) Method book() can directly call method cancelBooking() B) Method cancelBooking() can directly call method book() C) Hotel.book() is a valid invocation of book() D) Hotel.cancelBooking() is a valid invocation of cancelBooking() Answer C.D
7. Given the code. What is the result? public class SomeClass { private int value = 1; public void printVal(int value) { System.out.print(value); } public static void main(String args[]) { int a = 2; SomeClass c = new SomeClass(); c.printVal(a); } } A) "1" is printed B) "2" is printed C) Compilation fails D) An exception is thrown at runtime Answer : 2
8. Given the code. What is the result? public class Hotel { private static void book() { System.out.print("book"); } public static void main(String[] args) { Thread.sleep(1); book(); } } A) Compilation fails. B) An exception is thrown at runtime. C) "book" is printed. D) The code executes normally, but nothing is printed Answer : A 9. Give a piece of code. Which two are possible results? (Select two) public class Cruiser { private int a = 0; public void foo() { Runnable r = new LittleCruiser(); new Thread(r).start(); new Thread(r).start(); } public static void main(String arg[]) { Cruiser c = new Cruiser(); c.foo(); } public class LittleCruiser implements Runnable { public void run() { int current = 0; for (int i = 0; i < 4; i++) { current = a; System.out.print(current + ", ");
a = current + 2; } } } } A) 0, 2, 4, 0, 2, 4, 6, 6, B) 0, 2, 4, 6, 8, 10, 12, 14, C) 0, 2, 4, 6, 8, 10, 2, 4, D) 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14, E) 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14, Answer: B 10. Given the exhibit. What is the result? public class Hotel { public static void book(short a) { System.out.print("short "); } public static void book(Short a) { System.out.print("SHORT "); } public static void book(Long a) { System.out.print("LONG "); } public static void main(String[] args) { short shortRoom = 1; int intRoom = 2; book(shortRoom); book(intRoom); } } A) SHORT LONG B) short LONG
C) Compilation fails D) An exception is thrown at runtime Answer C
11. Given the code. What is the result? import java.io.*; public class Hotel implements Serializable { private Room room = new Room(); public static void main(String[] args) { Hotel h = new Hotel(); try { FileOutputStream fos = new FileOutputStream("Hotel.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(h); oos.close(); } catch(Exception ex) { ex.printStackTrace(); } } } class Room { } A) Compilation fails. B) An instance of Hotel is serialized. C) An instance of Hotel and an instance of Room are both serialized. D) An exception is thrown at runtime. Answer : B
12. Given the code. What is the result?
public static void main(String args[]) { try { String arr[] = new String[10]; arr = null; arr[0] = "one"; System.out.print(arr[0]); } catch(NullPointerException nex) { System.out.print("null pointer exception"); } catch(Exception ex) { System.out.print("exception"); } } A) "one" is printed. B) "exception" is printed. C) "null pointer exception" is printed. D) Compilation fails. Answer : D 13. Given the code. What is the result? import java.io.*; public class Hotel implements Serializable { private Room room = new Room(); public static void main(String[] args) { Hotel h = new Hotel(); try { FileOutputStream fos = new FileOutputStream("Hotel.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(h); oos.close(); } catch(Exception ex) { ex.printStackTrace(); } } } class Room implements Serializable { }
A) Compilation fails. B) An exception is thrown at runtime. C) An instance of Hotel is serialized. D) An instance of Hotel and an instance of Room are both serialized. Answer : C 14. Given the code. What is the output? 1. public static void main(String args[]) { 2. Object myObj = new String[]{"one", "two", "three"};{ 3. for (String s : (String[])myObj) System.out.print(s + "."); 4. } 5. } A) one.two.three. B) Compilation fails because of an error at line 2 C) Compilation fails because of an error at line 3 D) An exception is thrown at runtime. Answer : C 15. Given the code. What is the result? public class Test { private static void doStuff(String str) { int var = 4; if (var == str.length()) { System.out.print(str.charAt(--var) + " "); } else { System.out.print(str.charAt(0) + " "); } }
public static void main(String args[]) { doStuff("abcd"); doStuff("efg"); doStuff("hi"); } } A) Compilation fails. B) An exception is thrown at runtime. C) d e h D) d f i E) c f i F) c e h Answer : C 16. Which code, inserted inserted at line labeled "//some code goes her", allows the class Test to be compiled? class Util { public enum State{ACTIVE, DELETED, INACTIVE} } public class Test { public static void main(String args[]) { //some code goes here } } A) State state = State.INACTIVE; B) State state = INACTIVE; C) Util.State state = Util.State.INACTIVE; D) State state = Util.INACTIVE; Answer: B
17. Given the code. What is the result? import java.util.Collections; import java.util.LinkedList; import java.util.List; public class HashTest { private String str; public HashTest(String str) { this.str = str; } @Override public String toString() { return this.str; } public static void main(String args[]) { HashTest h1 = new HashTest("2"); String s1 = new String("1"); List