Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java CTS Dumps 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

1 Consider the following code:

1. public class GetArray2 {


2. public static void main(String [] args) {
3. int [][] holdit = new int[6][];
4. for(int x = 0;x<6;x++) {
5. holdit[x] = new int[3];
6. holdit[x][0] = (x + 0);
7. holdit[x][1] = (x + 1);
8. holdit[x][2] = (x + 2);
9. System.out.println(holdit[x][0]+" "+holdit[x][1]+" "+holdit[x][2]);
10. }
11. }
12. }

Which of the following gives the valid output for above?

Answer: a. Compilation fails because of an error on line 5

b. Compilation succeeds and the program prints:


012
123
234
345
456
567

c. Compilation fails because of an error on line 3

d. Compilation succeeds and the program prints:


111
222
333
444
555
666

2 Consider the following code:


public class Code9 {
public static void main(String args[]) {
System.out.println(Math.abs(Integer.MIN_VALUE));
}
}

Which of the following will be the output for the above given program? (Choose
2)

Answer: a. Compilation Error

b. Compiles successfully and prints a value which is less than


zero

c. Compiles successfully and prints a value which is equal to


Integer.MIN_VALUE

d. Compiles successfully and prints a value which is equal to -


Integer.MAX_VALUE

e. Compiles successfully and prints a value which is equal to


Integer.MIN_VALUE + 1

3 Consider the following scenario:

An application needs to write a log file on the sequence of actions it takes while
running.

Which of the following stream classes can be used to implement the above
requirement?

Answer: a. FileOutputStream

b. LogWriter

c. PrintWriter

d. OutputStream
e. Writer

4 Consider the following program:

import java.io.*;

public class SteppedTryCatch {


public static void main(String[] args) {
try {
try {
try {
// Line 1
} catch(Exception e3) {
System.out.println("Exception 1");
// Line 2
}
} catch(IOException e2) {
System.out.println("Exception 2");
// Line 3
}
} catch(FileNotFoundException e1) {
System.out.println("Exception 3");
}
}
}

You need to make the above program to print the output as


Exception 1
Exception 2
Exception 3

Which of the following when substituted in place of commented lines (// Line 1,
Line 2 and Line 3) produce the desired output?

Answer: a. Line 1 : throw new IOException();


Line 2 : throw new FileNotFoundException();
Line 3 : throw new Exception();
b. Line 1 : throw new Exception();
Line 2 : throw new IOException();
Line 3 : throw new FileNotFoundException();

c. The code is wrong. Exceptions should be caught in reversed


hierarchy order.

d. Line 1 : throw new IOException();


Line 2 : throw new IOException();
Line 3 : throw new IOException();

e. Line 1 : throw new FileNotFoundException();


Line 2 : throw new IOException();
Line 3 : throw new Exception();

5 What of the following is the default Scroll type for a ResultSet object?

Answer: a. ResultSet.TYPE_SCROLL_SENSITIVE

b. ResultSet.TYPE_SCROLLABLE

c. ResultSet.TYPE_SCROLL_INSENSITIVE

d. ResultSet.TYPE_FORWARD_ONLY

e. ResultSet.TYPE_SCROLL_BIDIRECTIONAL

6 Consider the following code snippet:

import java.io.*;

public class IOCode1 {


public static void main(String args[]) throws IOException {
BufferedReader br1 = new BufferedReader(
new InputStreamReader(System.in));
BufferedWriter br2 = new BufferedWriter(
new OutputStreamWriter(System.out));
String line = null;
while( (line = br1.readLine()) != null ) {
br2.write(line);
br2.flush();
}
br1.close();
br2.close();
}
}

What will be the output for the above code snippet?

Answer: a. Reads the text from keyboard character by character and


prints the same to the console on typing every character.

b. Reads the text from keyboard and prints the same to the
console on pressing Ctrl Z, flushes (erases) the same from the
console.

c. Reads the text from keyboard line by line and prints the same
to the console on pressing ENTER key at the end of every line

d. Reads the text from keyboard and prints the same to the
console on pressing Ctrl Z

e. Reads the text from keyboard line by line and prints the same
to the console on pressing ENTER key at the end of every line,
then the same is flushed (erased) from the console.

7 A monitor called 'mon' has 5 threads in its waiting pool; all these waiting
threads have the same priority. One of the threads is thread1. How can you
notify thread1 so that it alone moves from Waiting state to Ready State?

Answer: a. Execute mon.notify(thread1); from synchronized code of any


object

b. Execute notify(thread1); from within synchronized code of


mon
c. You cannot specify which thread will get notified

d. Execute thread1.notify(); from synchronized code of any


object

e. Execute thread1.notify(); from any code(synchronized or not)


of any object

8 Which of the following options are true about Associations?(choose 2)

Answer: a. Association refers to a class reuses the properties and


methods of another class

b. Association refers to an object composed of set of other


objects

c. Association refers to binding of related data and behaviours


into a single entity

d. Associations are bi-directional

e. In Associations, cardinality refers to the number of related


objects

9 Consider the following code snippet:

import java.util.*;

public class TestCol3 {


public static void main(String[] args) {
List l = new ArrayList();
l.add("One");
l.add("Two");
l.add("Three");
l.add("Four");
l.add("One");
l.add("Four");
Set h = new HashSet();
h.addAll(l);

System.out.println("Size:" + l.size() + h.size());


}
}

What will be the output of the following code snippet?

Answer: a. Size: 64

b. Size: 44

c. Size: 66

d. Compilation error

e. Size: 46

10 Consider the following code snippets:

class GC2 {
public GC2 getIt(GC2 gc2) {
return gc2;
}

public static void main(String a[]) {


GC2 g = GC2();
GC2 c = GC2();

c = g.getIt(c);
}
}

How many objects are eligible for Garbage Collection?

Answer: a. two
b. none of the objects are eligible

c. three

d. four

e. one

11 Which of the following gives the correct sequence of execution of callback


methods in a JUnit TestCase?

Answer: a. 1. setUp() method


2. testXXX() method
3. tearDown() method
Above sequence is for every testXXX() method in the TestCase

b. 1. setUp() method
2. tearDown() method
3. All testXXX() methods

c. 1. All testXXX() methods


2. setUp() method
3. tearDown() method

d. 1. setUp() method
2. All testXXX() methods
3. tearDown() method

12 Consider the following code:

class TM {
public static void main(String a[]) {
System.out.println(a[0]+a[0].length()+a.length);
}
}

Select the valid inputs and outputs. (Choose 2)


Answer: a. Input : java TM "Trick1"+"Trick2" "Trick3"
Output: Trick1+Trick2132

b. Input : java TM 'J2SE', 'J2EE', 'J2ME'


Output: J2SE43

c. Input : java TM 123+456 "abc" "Tricky"


Output: 57933

d. Input : java TM 'This is really tricky'


Output: This is really tricky211

e. Input : java TM "123", abc+234


Output: 123,42

13 Consider the following partial code:

public class CreditCard {


private String cardID;
private Integer limit;
public String ownerName;

public void setCardInformation(String cardID, String ownerName, Integer limit) {


this.cardID = cardID;
this.ownerName = ownerName;
this.limit = limit;
}
}

Which of the following statement is True regarding the above given code?

Answer: a. The class is fully encapsulated

b. The ownerName variable breaks encapsulation

c. The setCardInformation method breaks encapsulation

d. The cardID and limit variables break polymorphism


e. The code demonstrates polymorphism

14 State which of the following are default delimiters?(Choose 3)

Answer: a. form feed character

b. tab character

c. the dollar character

d. space character

e. the underscore character

15 Consider the following code snippet:

public class Demo extends Object {


String Title;

public Demo( String t ){


Title = t;
}

public void showTitle() {


System.out.println( "Title is " + Title );
}
}

class DerivedDemo extends Demo {


public void setTitle( String tt ) { Title = tt ; }
}

public class TasteIt {


public static void main(String args[]) {
DerivedDemo dd = new DerivedDemo();
dd.showTitle();
}
}

Which of the following option will be the output for the above code snippet?

Answer: a. Prints: "Title is null" to standard output.

b. A NullPointerException is thrown at Runtime

c. A ClassCastException is thrown at Runtime

d. Compilation Error

16 Consider the following code:

package com.java.test;
public class A {
public void m1() {System.out.print("A.m1, ");}
protected void m2() {System.out.print("A.m2, ");}
private void m3() {System.out.print("A.m3, ");}
void m4() {System.out.print("A.m4, ");}
}

class B {
public static void main(String[] args) {
A a = new A();
a.m1(); // 1
a.m2(); // 2
a.m3(); // 3
a.m4(); // 4
}
}
Assume that both of the above classes are stored in a single source file called
'A.java'. Which of the following gives the valid output of the above code?

Answer: a. Prints: A.m1, A.m2, A.m3, A.m4,

b. Compile-time error at 4.
c. Compile-time error at 3.

d. Compile-time error at 2.

e. Compile-time error at 1.

17 Consider the following code snippet:

import java.util.*;

public class TestCol6 {


public static void main(String args[] ){
ArrayList a = new ArrayList(); // Line 1
a.add(new Integer(10));
a.add(new String("Hello"));
a.add(new Double(34.9));
}
}

Which of the following code snippets when replaced at the line marked //Line 1,
will make the ArrayList a to accept only Wrapper types of primitive numerics?

Answer: a. ArrayList<Integer> a = new ArrayList<Integer>();

b. ArrayList<WrapperType> a = new ArrayList<WrapperType>();

c. ArrayList<Integer> a = new ArrayList<Double>();

d. ArrayList<Double> a = new ArrayList<Double>();

e. ArrayList<Number> a = new ArrayList<Number>();

Which of the following types of driver provides maximum decoupling between


database and Java application?
Answer: a. Type IV driver

b. Type II driver

c. Type I driver

d. Type III driver

19 Which of the following statements are true about String Arrays? (Choose 2)

Answer: a. Array index can be a negative value

b. String[][] s;

c. Array decaration: String[6] strarray;

d. String[][] s = new String[5][];

e. Array index can be a long value

20 Consider the following code:

public class WrapIt {


public static void main(String[] args) {
new WrapIt().testC('a');
}

public void testC(char ch) {


Integer ss = new Integer(ch);
Character cc = new Character(ch);
if(ss.equals(cc)) System.out.print("equals ");
if(ss.intValue()==cc.charValue()) {
System.out.println("EQ");
}
}
}
Which of the following gives the valid output for the above code?

Answer: a. Prints: EQ

b. Prints: equals

c. Compile-time error: Wrapper types cannot be compared


using equals

d. Prints: equals EQ

e. Compile-time error: Integer wrapper cannot accept char type

21 Consider the following code:

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();
}
}
Which of the following will be the output for the above program?
Answer: a. 3

b. No output

c. 321

d. 32

e. 123

22 Consider the following code:

public class Pass {


static int j=20;
public static void main(String argv[]) {
int i=10;
Pass p = new Pass();
p.amethod(i);
System.out.println(i);
System.out.println(j);
}

public void amethod(int x) {


x=x*2;
j=j*2;
}
}

Which of the following gives the correct output for the above code?

Answer: a. Prints: 10, 40

b. Prints: 20, 40

c. Compile time Error: Method parameter does not match


variable
d. Prints: 10,20

23 Consider the following code:

public class TH2 {


public static synchronized void main(String[] args) throws
InterruptedException {
Thread t = new Thread();
t.start();
System.out.print("keep");
t.wait(10000);
System.out.print("Smiling");
}
}

Which of the following gives the valid ouptut for the above code?

Answer: a. It prints keepSmiling with a 10000-second delay between


keep and Smiling.

b. It prints keepSmiling with a 10-second delay between keep


and Smiling

c. An exception is thrown at runtime

d. It prints keep and never exits

e. It prints keepSmiling and exits almost immeditately

24 Consider the following program:

class TestThread extends Thread {


public static void main(String apps[]) {
Thread t = null;
TestThread tc = new TestThread();
for (int i =0;i<5;i++) {
t = new Thread(tc);
// Insert Code Here
}
}

public void run() {


System.out.println("Hello");
}
}

Which of the following code when substituted to the commented line (//Insert
Code) will make the program to execute properly?

Answer: a. t.join()

b. t.start()

c. No additional code required

d. t.run()

e. t.sleep(1000)

25 Which of the following options give the names of the data structures that can
be used for Range-View operations, but no nulls?(choose 2)

Answer: a. SortedSet

b. List

c. Map

d. Vector

26 Consider the following code:


class A is defined in packageone as follows:

package com.packageone;
public class A {
private int x;

public A(int x) {
this();
this.x = x;
}

abstract void print();


}

And class B is defined in packagetwo as follows:

package com.packagetwo;
import com.packageone.A;
class B extends A {
B(int x) {
super(x);
}

void print() {
System.out.println(x);
}
}

class C {
public static void main(String args[]) {
A a = new B(10);

}
}

Which of the following changes will make the code to run properly? (Choose 3)

Answer: a. class B should be declared public abstract

b. class A should be declared public abstract


c. The print method in class B should refer the x as super.x

d. The member x in class A should be declared as protected

e. this() method call should be removed from the class A


constructor

27 Which of the following classes is used to handle the abnormal situation that
may occur during database calls using JDBC API?

Answer: a. SQLException

b. Driver

c. Connection

d. DriverManager

e. Statement

28 Consider the following code:

public class ThrowsException {


static void throwMethod() {
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("exception");
}

public static void main(String args[]) {


try {
throwMethod();
}
catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
}
}

Which of the following gives the output for the above given code?

Answer: a. Compiles successfully, nothing is printed

b. Compilation Error

c. Runtime Error

d. Inside showMethod. followed by caught:


java.lang.IllegalAccessException: exception

29 To which of the following elements, annotations can be applied? (Choose 3)

Answer: a. classes

b. fields

c. jar files

d. class files

e. methods

30 Consider the following:

Class A contains the following:

i. Instance Block
ii. main() method
iii. Static Block
iv. Default Constructor of A

Assume an instance of class A is created in the main() method. Which of the


following gives the correct sequence of execution of above mentioned
elements?

Answer: a. iii, ii, i, iv

b. iii, i, ii, iv

c. ii, i, iv, iii

d. i, iv, iii, ii

e. iv, iii, ii, i

31 Consider the following code snippet:

interface InterfaceOne {
int ID = 10;
int getAccess();
}

interface InterfaceTwo {
int ID = 20;
int getAccess();
}

class InterfaceImpl implements InterfaceOne, InterfaceTwo {


public int getAccess() {
if (this instanceof InterfaceOne) {
return InterfaceOne.ID;
} else {
return InterfaceTwo.ID;
}
}
}

public class Code {


public static void main(String args[]) {
InterfaceOne i1 = new InterfaceImpl();
System.out.println(i1.getAccess());
InterfaceTwo i2 = (InterfaceTwo) i1;
System.out.println(i2.getAccess());
}
}

Which of the following will be the output for the above code snippet?

Answer: a. 10
10

b. 10
20

c. Compile time error. Incompatible Type conversion.

d. 20
10

e. 20
20

32 Which of the following statements is true about NavigableSet interface?

Answer: a. a SortedSet extended with navigation methods for Maps.

b. a SortedSet extended with navigation methods for Lists.

c. a new class implementation of Set which can navigate the


ResultSet object

d. a SortedSet extended with navigation methods reporting


closest matches for given search targets.

33 Consider the following code:

class Planet { }
class Earth extends Planet { }

public class WelcomePlanet {


public static void welcomePlanet(Planet planet) {
if (planet instanceof Earth) {
System.out.println("Welcome!");
} else if (planet instanceof Planet) {
System.out.println("Planet!");
} else {
System.exit(0);
}
}

public static void main(String args[]) {


WelcomePlanet wp = new WelcomePlanet();
Planet planet = new Earth();
welcomePlanet(planet);
}
}

Which of the following will be the output of the above program?

Answer: a. The code runs with no output

b. Compilation fails

c. Welcome!

d. Planet!

e. An exception is thrown at runtime

34 Consider the following code:

1. public class Boxer1{


2. Integer i;
3. int x;
4.
5. public Boxer1(int y) {
6. x = i+y;
7. System.out.println(x);
8. }
9.
10. public static void main(String[] args) {
11. new Boxer1(new Integer(4));
12. }
13. }

Which of the following will be the output of the above program?

Answer: a. Compilation fails because of an error in line 11

b. A NullPointerException occurs at runtime

c. Prints: 4

d. A NumberFormatException occurs at runtime

35 Consider the following program:

1. class CheckedException extends RuntimeException { }


2. class UncheckedException extends Exception { }
3. public class Check {
4. public static void main(String args[]) {
5. generateException1();
6. generateException2();
7. }
8.
9. private static void generateException1() {
10. throw new CheckedException();
11. }
12.
13. private static void generateException2() {
14. throw new UncheckedException();
15. }
16. }
Which of the following is true regarding the above given program?

Answer: a. No compilation error but throws RuntimeException on


running the code

b. Compilation error at line 10

c. Compilation error at line 14

d. Compilation error at line 5

e. Compilation error at line 6

36 Consider the following code snippet:

public class TestString10{


public void print() {
String s = "Hello";
StringBuffer sb = new StringBuffer("Hello");
concatinateStrings(s, sb);
System.out.println(s+" "+sb);
}

public void concatinateStrings(String str, StringBuffer strBuff){


StringBuffer sk = strBuff;
str = str + " world";
sk.append(" world");
}

public static void main (String[] args) {


TestString10 t = new TestString10();
t.print();
}
}

What will be the output of the above code snippet?

Answer: a. Hello Hello Hello


b. world Hello Hello

c. Hello Hello world

d. Hello world Hello

e. world world world

37 Consider the following code:

package test;

class Target {
String name = "hello";
}

Which of the following options are valid that can directly access and change the
value of the variable 'name' in the above code? (Choose 2)

Answer: a. any class in the test package

b. any class that extends Target outside the test package

c. any class

d. only the Target class

e. any class that extends Target within the test package

38 Which of the following is the process of creating a new class from an existing
class?

Answer: a. Polymorphism

b. Inheritance
c. Abstraction

d. Reusability

39 Consider s1 and s2 are sets.

Which of the following options gives the exact meaning of the method call
s1.retainAll(s2)?

Answer: a. transforms s1 into the union of s1 and s2

b. transforms s1 into the intersection of s1 and s2.

c. copies elements from s2 to s1

d. returns true if s2 is a subset of s1

e. transforms s1 into the (asymmetric) set difference of s1 and


s2

40 Consider the following code segment:

public class ExampleThree {


public static void main(String args[]) {
int i = 1; // Line 1
short s = 1; // Line 2
long l = 1; // Line 3
i = l + i; // Line 4
l = s + i; // Line 5
}
}

Which of the following gives the line in the above program that will result in
error?
Answer: a. Line 4

b. Line 7

c. Line 6

d. Line 3

e. Line 5

41 Which of the following pre-defined annotations requires that an annotation


type should itself annotate with, in order to make the information in an user-
defined annotation type appear in Javadoc-generated documentation?

Answer: a. @Override

b. @Documented

c. @Comment

d. @Deprecated

42 Consider there are two threads, "Thread A" and "Thread B". "Thread A" holds a
lock on "Object X". "Thread B" is blocked inside a wait call on Object X.

Which of the following will make the "Thread B" runnable?

Answer: a. Thread A's wait() times out.

b. Thread A releases the lock on B by calling the notifyAll()

c. Thread A is interrupted.

d. Thread B is interrupted.
e. Thread B releases the lock on object X and calls the notify()
method on thread

43 Which of the following options are true? (Choose 2)

Answer: a. Error objects are thrown only by JVM

b. Errors can be thrown programmatically

c. A class can extend Error class and can be used as user-defined


Error class

d. Errors are handled only by JVM

e. Errors cannot be handled programmatically using try-catch


blocks.

44 Which of the following statements are true? (choose 2)

Answer: a. An object becomes eligible for garbage collection when all


references denoting it are set to null

b. The automatic garbage collection of the JVM prevents


programs from ever running out of memory

c. A program can suggest that garbage collection be performed


but not force it

d. None of the listed options

e. Garbage collection is platform independent

45 Which of the following statements are true regarding equals() method?(Choose


3)
Answer: a. Defined in the Object class

b. Essential for inheriting a class

c. Used for object's content comparison

d. It is polymorphic

e. Declared in the Object class

You might also like