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

Java CTS Dumps 2

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

1. Which of the following is the best-performing implementation of Set interface?

Answer: a. HashSet

b. TreeSet

c. Hashtable

d. LinkedHashSet

e. SortedSet

2 Consider the following code snippet:

public class Welcome {


String title;
int value;

public Welcome() {
title += " Planet";
}

public void Welcome() {


System.out.println(title + " " + value);
}

public Welcome(int value) {


this.value = value;
title = "Welcome";
Welcome();
}

public static void main(String args[]) {


Welcome t = new Welcome(5);
}
}

Which of the following option will be the output for the above code snippet?
Answer: a. Compilation fails

b. Welcome Planet 5

c. Welcome 5

d. Welcome Planet

e. The code runs with no output

3 Consider the following code:

class ExampleFive {
public static void main(String[] args) {
final int i = 22;
byte b = i;
System.out.println(i + ", " + b);
}
}

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

Answer: a. Prints: 22, 20

b. Runtime Error: Cannot type cast int to byte

c. Prints: 22, 22

d. Compile Time Error: Loss of precision

4 Consider the following code snippet:

abstract class Director {


protected String name;

Director(String name) {
this.name = name;
}
abstract void occupation();
}

class FilmDirector extends Director {


FilmDirector(String name) {
super(name);
}

void occupation() {
System.out.println("Director " + name + " directs films");
}
}

public class TestDirector {


public static void main(String[] args) {
FilmDirector fd = new FilmDirector("Manirathnam");
fd.occupation();
new Director("Manirathnam") {
void occupation() {
System.out.println("Director " + name + " also produces films");
}
}.occupation();
}
}

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

Answer: a. Prints: Director Manirathnam also produces films

b. Compilation fails at TestDirector class

c. Prints: Director Manirathnam directs films

d. Runtime Error

e. Prints: Director Manirathnam directs films


Director Manirathnam also produces films
5 Consider the following code snippet:

class TestString2 {
public static void main(String args[]) {
String s1 = "Test1";
String s2 = "Test2";

s1.concat(s2);

System.out.println(""+s1.charAt(s1.length() - 3) + s1.indexOf(s2));
}
}

What will be the output of the above code snippet?

Answer: a. s5

b. s-1

c. t4

d. 15

e. T4

6 Consider the following code:

1. public class SwitchIt {


2. public static void main(String[] args) {
3. int w1 = 1;
4. int w2 = 2;
5. System.out.println(getW1W2(w1, w2));
6. }
7.
8. public static int getW1W2(int x, int y) {
9. switch (x) {
10. case 1: x = x + y;
11. case 2: x = x + y;
12. }
13. return x;
14. }
15. }

Which of the following gives the valid output of above code?

Answer: a. Compilation succeeds and the program prints "5"

b. Compilation fails because of an error on line 9

c. Compilation succeeds and the program prints "3"

d. Compilation fails because of errors on lines 10 and 11

7 Consider the following code snippet:

class Train {
String name = "Shatapdhi";
}

class TestTrain {
public static void main(String a[]) {
Train t = new Train();
System.out.println(t); // Line a
System.out.println(t.toString()); // Line b
}
}

Which of the following statements are true?(Choose 3)

Answer: a. Line a prints the corresponding classname with Object's


hashcode in Hexadecimal

b. Both Line a and Line b prints "Shatapdhi"

c. Both Line a and Line b will print the corresponding classname


with Object's hashcode in Hexa Decimal

d. Output of Line a and Line b will be different


e. Output of Line a and Line b will be same

8 Which of the following is true about finalize() method?

Answer: a. The finalize() method is guaranteed to run once and only


once before the garbage collector deletes an object

b. finalize() is called when an object becomes eligible for


garbage collection

c. an object can be uneligibilize for GC from within finalize()

d. Calling finalize() method will make the object eligible for


Garbage Collection

e. finalize() method can be overloaded

9 Consider the following program:

class RE {
public static void main(String args[]) {
try {
String s = null;
System.out.println(s.length());
}
catch(NullPointerException npe) {
System.out.println("NullPointerException handled");
throw new Exception(npe.getMessage());
}
}
}

What will be the output of the above program?

Answer: a. Code compiles and on running creates and throws Exception


type object
b. Code compiles but run without any output

c. Code does not complile

d. Code compiles and on running it prints "NullPointerException


handled" then creates and throws Exception type object

10 Which of the following options will protect the underlying collections from
getting modified?

Answer: a. unmodifiableCollection(Collection<? extends T> c);

b. Collections.checked

c. synchronizedCollection(Collection<T> c);

d. None of the listed options

11 Which of the following are correct regarding HashCode?(Choose 2)

Answer: a. the numeric key is unique

b. It improves performance

c. it is a 32 bit numeric digest key

d. hashCode() value cannot be a zero-value

e. hashCode() is defined in String class

12 Which of the following are true about inheritance?(Choose 3)

Answer: a. In an inheritance hierarchy, a subclass can also act as a super


class
b. Inheritance enables adding new features and functionality to
an existing class without modifying the existing class

c. In an inheritance hierarchy, a superclass can also act as a sub


class

d. Inheritance is a kind of Encapsulation

e. Inheritance does not allow sharing data and methods among


multiple classes

13 Consider the following program:

public class TryIt {


public static void main(String args[]) {
try {
int i = 0;
try {
i = 100 / i;
}
catch(Error e) {
System.out.println("Divide by Zero error");
}
System.out.println("Error Handled");
}
catch(Exception e) {
System.out.println("Unexpected exception caught");
}
}
}

What will be the output of the above program?

Answer: a. Divide by Zero error


Error Handled
Unexpected exception caught

b. Program compiles and runs without any output


c. Divide by Zero error
Error Handled

d. Unexpected exception caught

e. Error Handled
Unexpected exception caught

14 At which of the following given situations, unit tests are required to be run on
the modules?(Choose 3)

Answer: a. When the modules are deployed to the server

b. When the other methods, referred in a method in a module


is modified

c. When class members are modified

d. When a method in a module is completed or modified

e. When the modules are compiled

15 Consider the following code:

1 public class A {
2 public void m1() {System.out.print("A.m1, ");}
3 protected void m2() {System.out.print("A.m2, ");}
4 private void m3() {System.out.print("A.m3, ");}
5 void m4() {System.out.print("A.m4, ");}
6
7 public static void main(String[] args) {
8 A a = new A();
9 a.m1();
10 a.m2();
11 a.m3();
12 a.m4();
13 }
14 }
Which of the following gives the lines that need to be removed in order to
compile and run the above code correctly?

Answer: a. Lines 10, 11

b. No need to comment any line. Will compile and run

c. Lines 10, 11 and 12

d. Line 11

16 Which of the following options give the names of data structures that can be
used for elements that have ordering, but no duplicates? (Choose 2)

Answer: a. ArrayList

b. List

c. TreeSet

d. Set

e. SortedSet

17 Which of the following class in java.sql package maps the SQL data types to Java
datatypes?

Answer: a. No explicit data type mapping. Automatically mapped on


Query Call.

b. JDBCTypes

c. Types

d. JDBCSQLTypes

e. SQLTypes
18 Which of the following options gives the difference between == operator and
equals() method?

Answer: a. Equals compares hash value and == compares character


sequence

b. No difference;they are essentially the same

c. if equals() is true then == is also true

d. ==compares object's memory address but equals character


sequence

e. == works on numbers equals() works on characters

19 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

b. any class that extends Target within the test package

c. any class that extends Target outside the test package

d. only the Target class

e. any class in the test package


20 Which of the following are the valid ways of conversion from Wrapper type to
primitive type? (Choose 3)

Answer: a. new Boolean("true").intValue();

b. new Double(24.5d).byteValue();

c. new Integer(1).booleanValue();

d. new Float(100).intValue();

e. new Integer(100).intValue();

21 Which of the following classes is new to JDK 1.6?

Answer: a. java.io.File

b. java.io.Serializable

c. java.io.FileFilter

d. java.io.Console

e. java.io.Externalizable

22 Which of the following statements are correct regarding Static Blocks?(Choose


3)

Answer: a. A class that have static block, should have the main() method
defined in it

b. A class can have more than one static block

c. Static blocks are executed only once

d. Static blocks are executed before main() method


e. A static block can call other methods in a class

23 Which of the following ways can be used to access the String value in the first
column of a ResultSet? (Assume rs is the ResultSet object)

Answer: a. rs.getString(1);

b. rs.getString("one");

c. None of listed options

d. rs.getString("first");

e. rs.getString(0);

24 Which of the following options gives the relationship between a Spreadsheet


Object and Cell Objects?

Answer: a. Polymorphism

b. Association

c. Aggregation

d. Inheritance

e. Persistence

25 Consider the following program:

class A implements Runnable {


public void run() {
System.out.print(Thread.currentThread().getName());
}
}
class B implements Runnable {
public void run() {
new A().run();
new Thread(new A(),"T2").run();
new Thread(new A(),"T3").start();
}
}

class C {
public static void main (String[] args) {
new Thread(new B(),"T1").start();
}
}

What will be the output of the above program?

Answer: a. Prints: T1T2T2

b. Prints: T1T2T3

c. Prints: T1T1T2

d. Prints: T1T1T1

e. Prints: T1T1T3

26 Consider the following program:

public class Exp4 {


static String s = "smile!..";
public static void main(String[] args) {
new Exp4().s1();
System.out.println(s);
}

void s1() {
try {
s2();
}
catch (Exception e) {
s += "morning";
}
}

void s2() throws Exception {


s3();
s += "evening";
s3();
s += "good";
}

void s3() throws Exception {


throw new Exception();
}
}

What will be the output of the above program?

Answer: a. smile!..eveningmorning

b. smile!..morningevening

c. smile!..morning

d. smile!..morningeveninggood

e. smile!..

27 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 thread1.notify(); from any code(synchronized or not)


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

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


mon

e. You cannot specify which thread will get notified

28 Consider the following scenario:

Here is part of the hierarchy of exceptions that may be thrown during file IO
operations:

Exception
+-IOException
+-File Not Found Exception

You have a method X that is supposed to open a file by name and read data
from it.
Given that X does not have any try-catch statements, which of the following
option is true?

Answer: a. The method X must be declared as throwing


FileNotFoundException

b. Any method calling X must use try-catch, specifically catching


FileNotFoundException

c. The method X must be declared as throwing IOException or


Exception

d. No special precautions need be taken

29 The return value of execute() method in Statement interface is


__________________.

Answer: a. array of int


b. boolean

c. ResultSet

d. array of ResultSet

e. int value

30 Consider the following program:

public class D extends Thread {


public void run() {
System.out.println("Before start method");
this.stop();
System.out.println("After stop method");
}

public static void main(String[] args) {


D a = new D();
a.start();
}
}

What will be the output of the above program?

Answer: a. 'Before start method' and 'After stop method'

b. Compilation error

c. 'Before start method' only

d. Runtime exception

31 Consider the following code:

public class Choco {


Choco() { System.out.print("Choco"); }
class Bar {
Bar() { System.out.print("bar"); }
public void go() { System.out.print("sweet"); }
}

public static void main(String[] args) {


Choco c = new Choco();
c.makeBar();
}
void makeBar(){
// Insert code here
}
}

Which of the following code snippet when substituted individually to the above
commented line (// Insert code here) will give the following output?

Chocobarsweet

Answer: a. new Choco().go();

b. new Bar().go();

c. new Choco(). new Bar().go();

d. (new Bar() {}).go();

e. go();

32 Which of the following actions include the external library required by Java
application at runtime in order to run properly? (choose 2)

Answer: a. Compressing the class into zip files and converting it into
executable modules, and then packaging it into a jar file

b. Adding the folder name or jar filename to the CLASSPATH


variable, where the class files of the library exists

c. Running the application with the following switches


java -cp <class search path of directories and zip/jar files>
ApplicationClassName
java -classpath <class search path of directories and zip/jar
files> ApplicationClassName

d. Cannot refer to an external libarary, it should be included


before the application is packaged.

e. Pointing the system's PATH variable to the folder or the jar


filename where the class files of the library exists

33 You need to create a class that implements the Runnable interface to do


background image processing. Out of the following list of method declarations,
select the method that must satisfy the requirements.

Answer: a. public void start()

b. public void run()

c. public void stop()

d. public void suspend()

34 Which of the following gives the set of Annotations declared in java.lang


package?

Answer: a. @Retention, @Target

b. @Deprecated, @Override, @SuppressWarning

c. @Comment, @Class

d. @Documented, @Inherited

35 Consider the following code:


class ABC {
public void method1() {
DEF def = new DEF();
def.method2();
}
}

class DEF {
public XYZ xyz;

public String method2() {


return xyz.method3();
}
}

class XYZ {
public String value;

public String method3() {


value = "XYZ";
return value;
}
}

class TestCode {
public static void main(String args[]) {
ABC abc = new ABC();
abc.method1();
}
}

Which of the following will be the output if the above code is compiled and
executed?

Answer: a. An exception is thrown at runtime

b. Prints: XYZ

c. Compilation fails
d. Prints: DEF

e. Prints: ABC

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

Answer: a. fields

b. jar files

c. classes

d. methods

e. class files

37 Consider the following scenario:

A Java application needs to stream a video from a movie file.

Which of the following options gives the correct combination of stream classes
that can be used to implement the above requirement?

Answer: a. LineInputStream and BufferedInputStream

b. FileReader and BufferedReader

c. InputStreamReader and FileInputStream

d. FileInputStream and FilterInputStream

e. FileInputStream and BufferedInputStream

38 Consider the following code:


public class SwitchIt {
public static void main(String args[]) {
int x = 10;

switch(x) {
case 10:
for(int i=0; i<x; ++i)
break;

case 20:
System.out.println(x);
break;

case 30:
System.out.println(x*2);
break;

default:
System.out.println(x*3);
}
}
}

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

Answer: a. 10

b. 11

c. No output

d. 30

39 Consider the following code:

class A { }
class B extends A { }
public class Code2 {
public void method(A a) {
System.out.println("A");
}
public void method(B b) {
System.out.println("B");
}
public static void main(String args[]) {
new Code2().method(new Object());
}
}
Which of the following will be the output for the above code?

Answer: a. Prints: B

b. Throws ClassCastException at runtime

c. Compilation Error 'Cannot find the symbol'

d. Prints: A

40 Consider the following code snippet:

class Lock1 {
Lock1() { }
Lock1(Lock2 lock2) { this.lock2 = lock2; }
Lock2 lock2;
}

class Lock2 {
Lock2() { }
Lock2(Lock1 lock1) { this.lock1 = lock1; }
Lock1 lock1;
}

class GC6 {
public static void main(String args[]) {
Lock1 l1 = new Lock1();
Lock2 l2 = new Lock2(l1);
l1.lock2 = l2;
}
}
Which of the objects are eligible for garbage collection in the above code?

Answer: a. l1

b. l2

c. None of the objects eligible

d. lock1

e. lock2

41 Consider the following code snippet:

import java.io.*;

class Student {
private String studentID;
private String studentName;

public Student() { }
public Student(String studentID, String studentName) {
this.studentID = studentID;
this.studentName = studentName;
}

public String toString() {


return "Student ID : " + studentID + " " +
"Student Name : " + studentName;
}
}

public class IOCode3 {


public static void main(String args[]) throws FileNotFoundException,
IOException {
ObjectOutputStream out = new ObjectOutputStream(new
FileOutputStream("C:/ObjectData")); // Line 1
out.writeObject(new Student("100", "Student One")); // Line 2
out.close();
}
}

What will be the output of the above code snippet?

Answer: a. Code will compile and execute without any error

b. throws IOException at // Line 1

c. throws IOException at // Line 2

d. throws NotSerializableException at // Line 2

e. throws NotSerializableException at // Line 1

42 Consider the following code snippet:

interface i1 {
int i = 0;
}

interface i2 {
int i = 0;
}

class inter implements i1, i2 {


public static void main(String[] a) {
System.out.println(i);
}
}

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

Answer: a. No output

b. Prints: 0

c. Runtime Error
d. Compilation Error

43 Consider the following code:

1. class ExampleSix {
2. String msg = "Type is ";
3. public void showType(int n) {
4. String tmp;
5. if(n > 0) tmp = "positive";
6. System.out.println(msg + tmp);
7. }
8. }

On running the above code it throws the compile-time error- the variable tmp is
not initialised.

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

Answer: a. Delcare the variable tmp as StringBuffer type

b. Declare the variable tmp as static

c. Insert the following line at line 6


else tmp = "not positive";

d. Change line 4 as follows


String tmp = null;

e. Remove line 4 and insert it at line 2

44 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. 321

b. No output

c. 123

d. 32

e. 3

45 Consider the following code snippet:

class Student {
String studentID;
String studentName;
double score;
}

The instances of the above defined class holds the information of individual
student. These details needs to be maintained in a data structure, that

* allows searching of student details using studentID


* ascending order of Student objects based on studentID

Which of the following collection classes can be used to implement the above
scenario?

Answer: a. TreeSet

b. HashMap

c. HashSet

d. ArrayList

e. TreeMap

You might also like