Java Programming Objective Questions 1
Questions by Rejinpaul.com
1.Which will legally declare, construct, and initialize an array?
a)int [] myList = {"1", "2", "3"};
b)int [] myList = (5, 8, 2);
c) int myList [] [] = {4,9,7,0};
d)int myList [] = {4, 3, 7};
The Correct Answer is:
d)int myList [] = {4, 3, 7}
Explanation:
The only legal array declaration and assignment statement is Option D
Option A is wrong because it initializes an int array with String literals.
Option B is wrong because it use something other than curly braces for the initialization.
Option C is wrong because it provides initial values for only one dimension,
although the declared array is a two-dimensional array.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
2.Which is a reserved word in the Java programming language?
a)method
b)native
c) subclasses
d)reference
The Correct Answer is:
b)native
Explanation:
The word "native" is a valid keyword, used to modify a method declaration
Option A, D and E are not keywords. Option C is wrong because the keyword for
subclassing in Java is extends, not 'subclasses'.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
3.Which is a valid keyword in java?
a)interface
b) string
c) Float
d)unsigned
The Correct Answer is:
a)interface
Explanation:
interface is a valid keyword.
Option B is wrong because although "String" is a class type in Java, "string" is not a keyword.
Option C is wrong because "Float" is a class type. The keyword for the Java primitive is float.
Option D is wrong because "unsigned" is a keyword in C/C++ but not in Java.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
4.Which is a valid declarations of a String?
a)String s1 = null;
b)String s2 = 'null';
c)String s3 = (String) 'abc';
d) String s4 = (String) '\ufeed';
The Correct Answer is:
a)String s1 = null;
Explanation:
Option A sets the String reference to null.
Option B is wrong because null cannot be in single quotes.
Option C is wrong because there are multiple characters between the single quotes ('abc').
Option D is wrong because you can't cast a char (primitive) to a String (object).
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
5.What is the numerical range of a char?
a)-128 to 127
b)-(2^15) to (2^15) - 1
c) 0 to 32767
d)0 to 65535
The Correct Answer is:
d)0 to 65535
Explanation:
A char is really a 16-bit integer behind the scenes, so it supports 216 (from 0 to
65535) values.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
6.What is the name of the method used to start a thread execution?
a)init();
b)start();
c) run();
d)resume();
The Correct Answer is:
b)start();
Explanation:
Option B is Correct. The start() method causes this thread to begin execution;
the Java Virtual Machine calls the run method of this thread.
Option A is wrong. There is no init() method in the Thread class.
Option C is wrong. The run() method of a thread is like the main() method to an
application. Starting the thread causes the object's run method to be called in that
separately executing thread.
Option D is wrong. The resume() method is deprecated. It resumes a suspended thread.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
7. class X implements Runnable
{
public static void main(String args[])
{
/* Missing code? */
}
public void run() {}
}
a)Thread t = new Thread(X);
b)Thread t = new Thread(X); t.start();
c)X run = new X(); Thread t = new Thread(run); t.start();
d)Thread t = new Thread(); x.run();
The Correct Answer is:
c)X run = new X(); Thread t = new Thread(run); t.start();
Explanation:
Option C is suitable to start a thread.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
8.Which method registers a thread in a thread scheduler?
a)run();
b)construct();
c)start();
d)register();
The Correct Answer is:
c)start();
Explanation:
Option C is correct. The start() method causes this thread to begin execution; the Java Virtual Machine calls the
run method of this thread.
Option A is wrong. The run() method of a thread is like the main() method to an
application. Starting the thread causes the object's run method to be called in that
separately executing thread.
Option B is wrong. There is no construct() method in the Thread class.
Option D is wrong. There is no register() method in the Thread class.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
9.Which class or interface defines the wait(), notify(),and notifyAll() methods?
a)Object
b) Thread
c)Runnable
d)Class
The Correct Answer is:
a)Object
Explanation:
The Object class defines these thread-specific methods.
Option B, C, and D are incorrect because they do not define these methods. And
yes, the Java API does define a class called Class, though you do not need to
know it for the exam.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
10. void start() {
A a = new A();
B b = new B();
a.s(b);
b = null; /* Line 5 */
a = null; /* Line 6 */
System.out.println("start completed"); /* Line 7
*/ }
When is the B object, created in line 3, eligible for
garbage collection?
a)after line 5
b) after line 6
c)after line 7
d)There is no way to be absolutely certain.
The Correct Answer is:
d)There is no way to be absolutely certain
Explanation:
No answer description available for this question
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
11.Which constructs an anonymous inner class instance?
a) Runnable r = new Runnable() { };
b)Runnable r = new Runnable(public void run() { });
c)Runnable r = new Runnable { public void run(){}};
d)System.out.println(new Runnable() {public void run() { }});
The Correct Answer is:
d)System.out.println(new Runnable() {public void run() { }});
Explanation:
D is correct. It defines an anonymous inner class instance, which also means it creates an instance of that new anonymous class at the same time. The anonymous class is an implementer of the Runnable interface, so it must override the run() method of Runnable.
A is incorrect because it doesn't override the run() method, so it violates the rules of interface implementation.B and C use incorrect syntax.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
12.Which class does not override the equals() and hashCode() methods, inheriting
them directly from class Object?
a) java.lang.String
b)java.lang.Double
c)java.lang.StringBuffer
d)java.lang.Character
The Correct Answer is:
c) java.lang.StringBuffer
Explanation:
java.lang.StringBuffer is the only class in the list that uses the default
methods provided by class Object.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
13.Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?
a)java.util.HashSet
b)java.util.LinkedHashSet
c)java.util.List
djava.util.ArrayList
The Correct Answer is:
d)java.util.ArrayList
Explanation:
All of the collection classes allow you to grow or shrink the size of your collection.
ArrayList provides an index to its elements. The newer collection classes tend not
to have synchronized methods. Vector is an older implementation of ArrayList
functionality and has synchronized methods; it is slower than ArrayList.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
14. Which interface does java.util.Hashtable implement?
a)Java.util.Map
b)Java.util.List
c)Java.util.HashTable
d)Java.util.Collection
The Correct Answer is:
a)Java.util.Map
Explanation:
Hash table based implementation of the Map interface.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
15.Which interface provides the capability to store objects using a key-value pair?
a) Java.util.Map
b)Java.util.Set
c)Java.util.List
d)Java.util.Collection
The Correct Answer is:
a) Java.util.Map
Explanation:
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
16.Which collection class allows you to associate its elements with key values, and allows you to retrieve objects in FIFO (first-in, first-out) sequence?
a)java.util.ArrayList
b)java.util.LinkedHashMap
c)java.util.HashMap
java.util.TreeMap
The Correct Answer is:
b)java.util.LinkedHashMap
Explanation:
LinkedHashMap is the collection class used for caching purposes. FIFO is another
way to indicate caching behavior. To retrieve LinkedHashMap elements in cached
order, use the values() method and iterate over the resultant collection.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
17.Which collection class allows you to access its elements by associating a key with an
element's value, and provides synchronization?
a)java.util.SortedMap
b)java.util.TreeMap
c)java.util.TreeSet
d)java.util.Hashtable
The Correct Answer is:
d)java.util.Hashtable
Explanation:
Hashtable is the only class listed that provides synchronized methods. If you need
synchronization great; otherwise, use HashMap, it's faster.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
18.Which interface provides the capability to store objects using a key-value pair?
a)Java.util.Map
b)Java.util.Set
c)Java.util.List
d)Java.util.Collection
The Correct Answer is:
a)Java.util.Map
Explanation:
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
19.Which collection class allows you to associate its elements with key values, and allows you to retrieve objects
in FIFO (first-in, first-out) sequence?
a)java.util.ArrayList
b)java.util.LinkedHashMap
c) java.util.HashMap
d)java.util.TreeMap
The Correct Answer is:
b) java.util.LinkedHashMap
Explanation:
LinkedHashMap is the collection class used for caching purposes. FIFO is another
way to indicate caching behavior. To retrieve LinkedHashMap elements in cached
order, use the values() method and iterate over the resultant collection.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
20. Which collection class allows you to access its elements by associating a key with an element's value, and
provides synchronization?
a) java.util.SortedMap
b)java.util.TreeMap
c)java.util.TreeSet
d)java.util.Hashtable
The Correct Answer is:
d)java.util.Hashtable
Explanation:
Hashtable is the only class listed that provides synchronized methods. If you need
synchronization great; otherwise, use HashMap, it's faster.
◊ View answer
◊ Share This In FB
_____________________________________________________________________________________________________________________
Pages:
1
2
3
4
Newer Post
Older Post
Home
Ad Inside Post
Comments system
Disqus Shortname