public class ThrowsDemo
{
static void throwMethod()
{
System.out.println(“Inside throwMethod.”);
throw new IllegalAccessException(“demo”);
}
public static void main(String args[])
{
try
{
throwMethod();
}
catch (IllegalAccessException e)
{
System.out.println(“Caught ” + e);
}
}
}
Choices:
a. Compilation error
b. Runtime error
c. Compile successfully, nothing is printed.
d. Inside throwMethod. followed by caught:
java.lang.IllegalAccessExcption: demo
Answer:______
**———————————————————————————————–**
2.What will happen when you attempt to run the following code from command line using command
– java AssertTest?(Assume that the code is compiled with assertions enabled)
1. public class AssertTest
2. {
3. static int i = 10;
4. public static void main(String args[])
5. {
6. i = i*2;
7. try
8. {
9. assert isValid() : i = i/4;
10. }catch(AssertionError ignore){}
11.
12. System.out.println(“i = ” +i);
13. }
14.
15. public static boolean isValid()
16. {
17. i = i * 2;
18. return false;
19. }
20. }
A. It will print – i = 20
B. It will print – i = 10
C. It will print – i = 40
D. It will print – i = 5
E. None of these
Answer:______
**———————————————————————————————–**
3.What will happen when you attempt to compile and run the following code?
public class Static
{
static
{
int x = 5;
}
static int x,y;
public static void main(String args[])
{
x–;
myMethod();
System.out.println(x + y + ++x);
}
public static void myMethod()
{
y = x++ + ++x;
}
}
Choices:
a. Compiletime error
b. prints : 1
c. prints : 2
d. prints : 3
e. prints : 7
f. prints : 8
Answer:______
**———————————————————————————————–**
4.You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order. Which interface provides that capability?
A. java.util.Map
B. java.util.Set
C. java.util.List
D. java.util.StoredSet
E. java.util.StoredMap
F. java.util.Collection
Answer:______
**———————————————————————————————–**
5.What should be inserted at line 7 so that the code compiles and guarantees to print – Apple Cricket Banana in the same order.
1. import java.util.*;
2. public class SetTest4. {
5. public static void main(String args[]{
// what should be inserted here?
set.add(“Apple”);10.
set.add(“Cricket”);
set.add(“Banana”);
Iterator iter = set.iterator();
while(iter.hasNext()){
System.out.print(iter.next() + ” “); }
}
}
A. Set set = new linkedHashSet();
B. Set set = new HashSet();
C. Set set = new TreeSet();
D. TreeSet maintains the Set elements sorted according to their natural order; however other sets do not guarantee iteration order of the set. In particular, they do not guarantee that the order will remain constant over time.
Answer:______
**———————————————————————————————–**
6.Which Java collection class can be used to maintain the entries in the order in which they were last accessed?
A. java.util.HashSet
B. java.util.linkedHashMap
C. java.util.Hashtable
D. java.util.VectorE. None of these
Answer:______
**———————————————————————————————–**
7. import java.util.*;
public class Pippin {
public static void main(String argv[]){
TreeMap tm = new TreeMap();
tm.put(“one”, new Integer(1));
tm.put(“two”,new Integer(3));
tm.put(“three”,new Integer(2));
Iterator it = tm.keySet().iterator();
while(it.hasNext()){
Integer iw = tm.get(it.next());
System.out.print(iw);
}
}
}
A Compile time error at line 10.
B Compilation and output of 123
C Compilation and output of the digits “1”, “2” and “3”, but the order can’t be determined.
D Compilation and output of onetwothree
E Compilation but runtime error at line 10.
Answer:______
**———————————————————————————————–**
8.[Select All]
[a]Vector filelist = ((Directory) file).getList();
[b]String[] filelist = file.directory();
[c]Enumeration filelist = file.contents();
[d]String[] filelist = file.list();
[e]Vector filelist = (new Directory(file)).files();
[Correct Choices]
Answer:______
**———————————————————————————————–**
9.Which most closely matches a description of a Java Map?
a) A vector of arrays for a 2D geographic representation
b) A class for containing unique array elements
c) A class for containing unique vector elements
d) An interface that ensures that implementing classes cannot contain duplicate keys
Answer:______
**———————————————————————————————–**
10.Given the uncompleted method:
1)
2) { success = connect();
3} if (success==-1) {
4} throw new TimedOutException();
5} }
6}}
TimedOutException is not a RuntimeException. Which can complete the method of declaration when added at line 1?
A. public void method()
B. public void method() throws Exception
C. public void method() throws TimedOutException
D. public void method() throw TimedOutException
E. public throw TimedOutException void method()
Answer:______
1A
2A
3D
4D
5A
6Answer: B
7A The compiler realizes that the get returns an Object reference which needs a cast to type Integer.
8[d]
9d
10bc



