Question:* Which one of the following is NOT a reserved word in Java?
Answer: • virtual
Question:* Why is it important to override hashCode() when you override equals()?
Answer: • Overriding equals without hashCode breaks the contract of hashCode().
Question:* What is the issue with the following code? String s = ""; for(int i = 0; i < 1000000; i++) { s += Integer.toString(i); }
Answer: • It will perform very slowly because strings are immutable.
Question:* Complete the sentence: One may extend a class from ...
Answer: • Only one non-final class
Question:* Which one of these lists contains only Java programming language keywords?
Answer: • class, if, void, long, int, continue
Question:* How should you create a new class that maps keys to values, using the Java Collections Framework?
Answer: • Implement the Map interface, possibly by extending the AbstractMap class
Question:* The Object.wait() method:
Answer: • Causes the current thread to wait
Question:* A java class which extends another class is usually described with the word:
Answer: • subclass
Question:* Which of these are advantages of encapsulation in Java?
Answer: • All of these
Question:* To document an API, which tool do you use?
Answer: • javadoc
Question:* If we want a class not to be overridden,the class must be done as
Answer: • Class should be final
Question:* The "javac" command line tool is used to:
Answer: • Compile java source files into bytecode class files
Question:* Which additional keyword may be used with try-catch blocks?
Answer: • finally
Question:* The most reliable way to compare two Strings for equality is by:
Answer: • Using the .equals() or .compareTo() method of one object on the other
Question:* Java handles memory allocation and reuse using a process called:
Answer: • Garbage Collection
Question:* The part of a "try" block that is always executed is:
Answer: • "finally"
Question:* To define a child class from the Parent class following is used:
Answer: • class Child extends Parent
Question:* What is an example of proper capitalization for a class name?
Answer: • CamelCase
Question:* If a method or variable is marked as having the "private" access level, then it can only be accessed from:
Answer: • Inside the same class
Question:* What is the most efficient way to concatenate a large number of strings in Java?
Answer: • The StringBuffer object.
Question:* Finally is used to....
Answer: • ensure a block of code is always executed after a try/catch
Question:* Which of the following is a valid constructor signature?
Answer: • public className()
Question:* When you create a thread with the “new” operator – which one of the following statements is true about its state
Answer: • it will be “runnable” when start() method is called
Question:* The Thread.sleep() method:
Answer: • Causes the current thread to suspend execution
Question:* How can you stop your class from being inherited by another class?
Answer: • Declare the class as final.
Question:* Can an abstract class be a final class?
Answer: • No
Question:* What is the correct way to create an instance of a class?
Answer: • ClassName varName = new ClassName(arguments);
Question:* True of False? The strictfp keyword ensures that you get the same result on every platform if you perform operations in the floating point variable.
Answer: • True
Question:* Interfaces are useful for...
Answer: • creating a design contract that encapsulates implementation
Question:* Which of these is true?
Answer: • A class implements an interface but extends a class
Question:* How can we use the class or jar files kept on the network path, within our projects?
Answer: • Including the path and class /jar file name in the Classpath
Question:* What is auto boxing?
Answer: • JVM conversion between primitive types and reference types
Question:* The "java" command line tool is used to:
Answer: • Load and execute java .class files
Question:* The Core Java platform provides many benefits to developers, including:
Answer: • A consistent programming interface across multiple hardware platforms
Question:* What method should you always override when you have overridden the equals() method?
Answer: • hashCode()
Question:* JDBC addresses the issue of transactions.
Answer: • True
Question:* The "static" keyword marks something as:
Answer: • Belonging to a class, rather than a specific instance
Question:* The instanceof operator can be used to determine if an object is:
Answer: • (All of these)
Question:* What are all the different types of access modifiers in Java
Answer: • private, protected, default, public
Question:* What is the benefit of ConcurrentHashMap<K,V>?
Answer: • All operations are thread-safe and retrieval operations do not entail locking
Question:* You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective?
Answer: • protected
Question:* When creating a user defined class for storing objects in a HashMap, which method(s) should be overridden?
Answer: • Both the equals() and hashCode() methods
Question:* When the == comparator is used on two objects, it checks to see if they:
Answer: • Are references to exactly the same object
Question:* Java provides a class for mutable sequences of characters, called:
Answer: • StringBuffer
Question:* What is the difference between a checked and unchecked exception?
Answer: • Checked exceptions must be caught while unchecked do not need to caught
Question:* An "overloaded" method has what in common with one (or more) methods on the same class?
Answer: • The same name
Question:* The List interface has which superinterfaces?
Answer: • Both Collection and Iterable
Question:* Calling System.gc() when using a modern JVM :
Answer: • Does not necessarily force garbage collection to occur, and is not idiomatic java.
Question:* Which one of the following statements is true about Java Beans?
Answer: • Java Beans are user defined classes.
Question:* Which class/classes is/are thread safe among these?
Answer: • String and StringBuffer
Question:* Which option is true for StringBuffer and StringBuilder
Answer: • StringBuffer are thread safe and StringBuilder are not thread safe
Question:* Which option is true for StringBuffer and StringBuilder
Answer: • StringBuffer are thread safe and StringBuilder are not thread safe
Question:* What is the output? int[] xxx = {10, 20}; List<String> list = new ArrayList<String>(10); list.add("01"); list.add("02"); System.out.println(xxx.length + ", " +list.size());
Answer: • 2, 2
Question:* What is a LinkedHashSet?
Answer: • A hash set which preserves the order in which objects were inserted.
Question:* Immutable objects are always...
Answer: • thread safe
Question:* A method without an access modifier (i.e. public, private, protected) is...
Answer: • package-private
Question:* In addition to CORBA, Core Java also supports network services using:
Answer: • Remote Method Invocation
Question:* Java's automatic memory management:
Answer: • Can be tuned using Virtual Machine settings
Question:* Java's String class is
Answer: • Final, with immutable instances
Question:* enum Example { ONE, TWO, THREE } Which statement is true?
Answer: • The expressions (ONE == ONE) and ONE.equals(ONE) are both guaranteed to be true.
Question:* Which will compile successfully?
Answer: • String s = (String)Old.get(new LinkedList<String>());
Question:* A class implementing a singleton pattern has...
Answer: • no public constructors, a public static factory method, a static instance variable.
Question:* Which of the following is true about overloading vs overriding methods?
Answer: • Overloading happens at compile time, while overriding happens at runtime
Question:* What is the result when this code is executed: class One { public One() { System.out.print(1); } } class Two extends One { public Two() { System.out.print(2); } } class Three extends Two { public Three() { System.out.print(3); } } public class Numbers{ public static void main( String[] argv ) { new Three(); } }
Answer: • 123
Question:* How do you convert int[] to a ArrayList<Integer>?
Answer: • Using the static Arrays.asList method
Question:* What is a weak reference?
Answer: • A reference to an object which may have been garbage collected when the object is asked for.
Question:* Can the "main" method be overloaded
Answer: • Yes
Question:* How many objects are created: String s1="String"; String s2="String"; String s3="String";
Answer: • One
Question:* What Object would you use to represent currency ?
Answer: • BigDecimal
Question:* What type should you use for floating point monetary calculations?
Answer: • BigDecimal
Question:* What will be printed out if you attempt to compile and run the following code? int i=9; switch (i) { default: System.out.println("default "); case 0: System.out.println("zero "); break; case 1: System.out.println("one "); case 2: System.out.println("two "); }
Answer: • default zero
Question:* After the following code fragment, what is the value in a? String s; int a; s = "Foolish boy."; a = s.indexOf("fool");
Answer: • -1
Question:* public static void append(List list) { list.add("0042"); } public static void main(String[] args) { List<Integer> intList = new ArrayList<Integer>(); append(intList); System.out.println(intList.get(0)); } What is the result?
Answer: • 0042
Question:* To create a single instance of a class, we can go with
Answer: • (none of these)
Question:* When must you override hashcode and equals method?
Answer: • For better performance when you want to use the object as a key in a HashMap
Question:* what is the output of following code? class A { int x=12; public static void main(String args[]) { x++; System.out.println(x); } }
Answer: • Compile time Error.
Question:* Anonymous inner classes have access to...
Answer: • Final, local variables in the containing scope
Question:* Which statement is true?
Answer: • catch(X x) can catch subclasses of X where X is a subclass of Exception.
Question:* In your program, you need to read a zip file (myfile.zip) containing several other data files containing basic Java objects. Which of the following will allow you to construct a InputStream for the task?
Answer: • new ObjectInputStream(new ZipInputStream( new FileInputStream((“myfile.zip”)));
Question:* Does interrupt() always force all threads to terminate?
Answer: • No, if the interruption is not enabled for the thread, it will not terminate
Question:* The documentation comment starts and ends with
Answer: • /** and */
Question:* Float p = new Float(3.14f); if (p > 3) { System.out.print("p is bigger than 3. "); } else { System.out.print("p is not bigger than 3. "); } finally { System.out.println("Have a nice day."); } What is the result?
Answer: • Compilation fails.
Question:* Which of the following statements about static inner classes is true?
Answer: • A static inner class has no reference to an instance of the enclosing class.
Question:* All of the classes in the Java Collections Framework:
Answer: • Have methods to retrieve their data as an Array
Question:* Given the code: Integer i= new Integer("1"); if (i.toString() == i.toString()) System.out.println("Equal"); else System.out.println("Not Equal");
Answer: • Prints "Not Equal"
Question:* which statement is True ?
Answer: • The notifyAll() method must be called from a synchronized context.
Question:* Which one of the following statements is true about threads in Java
Answer: • the notify() method can only be called from inside a synchronized block or from a synchronized method
Question:* java.util.Collection is:
Answer: • An interface for iterable groups of objects
Question:* A "blank" final variable (defined without an initial value):
Answer: • Can be initialized later, but only in a single location
Question:* LinkedBlockingQueue is useful for...
Answer: • implementing a producer consumer pattern
Question:* The keyword that ensures a field is coherently accessed by multiple threads is:
Answer: • "volatile"
Question:* ¿If the class X extends the class Y, wich of these is a polymorphic sentence?
Answer: • Y y1= new X(); y1.methodOfY();
Question:* The TreeMap class is Java's implementation of which data structure?
Answer: • Red-Black tree
Question:* I would implement a LRU cache using only JDK classes by...
Answer: • A TreeMap and iteration over values that containing a last accessed timestamp
Question:* Which code fragments correctly create and initialize a static array of int elements?
Answer: • static final int[] a; static { a=new int[2]; a[0]=100; a[1]=200; }
Question:* Which of the following are not a valid declarations?
Answer: • float f = 1.2;
Question:* You need to keep a list of one million objects sorted at all times having 100 random inserts and delete per second. Reads from the list are always done in sorted order. You would:
Answer: • Use a PriorityQueue to keep the list ordered at all times.
Question:* Livelock describes a situation in which two or more threads block each other, because:
Answer: • Their actions are also responses to the actions of others, such that all are too busy to respond
Question:* Which of the following is true about the Cloneable interface?
Answer: • It changes the behavior of the protected clone method to give a field-by-field (reference) copy of the object.
Question:* You want to listen TCP connections on port 9000. How would you create the socket?
Answer: • new ServerSocket(9000);
Question:* The TreeMap and LinkedHashMap classes:
Answer: • Enable iteration of a map's entries in a deterministic order.
Question:* public class Test{ public static void main(String [] arg){ int x=10; if(x++ > 10 && x++ == 12){ ++x; } System.out.println(x); } } What is the Output of this code?
Answer: • 11
Question:* A Guarded Block is a concurrency idiom in which:
Answer: • A condition is polled before the execution of a code block can proceed
Question:* What's the output of following error ? class A { public Number getNumber(){ return 1; } } class B extends A { public int getNumber(){ return 2; } } public class Main{ public static void main(String []args){ A a = new B(); System.out.println(a.getNumber()); } }
Answer: • Compilation error
Question:* A class may extend:
Answer: • Only one non-final class
Question:* What is the difference between a checked and unchecked exception?
Answer: • Checked exceptions must be caught while unchecked do not need to be caught
Question:* What is the name of the method used to start a thread execution?
Answer: • start();
Question:* Java variables are passed into methods as:
Answer: • Pass-by Reference
Question:* What is @Override annotation used for?
Answer: • It makes compiler check that method is really overridden
Question:* What is the correct syntax for importing java.util.Scanner?
Answer: • import java.util.Scanner;
Question:* What is the output? public static void main(String[] args) { int x=10; if(x++ > 9 && x++ == 12){ ++x; } System.out.println(x); }
Answer: • 12
Question:* What will be the output of this code? class Main { static abstract class Base { protected Base() { init(); } abstract void init(); } static class Child extends Base { private final int value; public Child() { value = 5; } @Override public void init() { System.out.println("value = " + value); } } public static void main(String[] args) { Child c = new Child(); } }
Answer: • value = 0
No comments:
Post a Comment