0% found this document useful (0 votes)
76 views

Java CTS Dumps 4

The document contains 13 multiple choice questions related to Java programming concepts. Some key points covered include: - Uses of the Object class - Properties and capabilities of StringBuffer - Differences between toString() and println() output - Features and appropriate uses of PreparedStatement - Effects of passing parameters by value vs reference in methods - Scenarios that throw checked exceptions - Requirements for thread synchronization calls like wait() - Outcomes of casting and inheritance between classes

Uploaded by

TRICKY MIND
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Java CTS Dumps 4

The document contains 13 multiple choice questions related to Java programming concepts. Some key points covered include: - Uses of the Object class - Properties and capabilities of StringBuffer - Differences between toString() and println() output - Features and appropriate uses of PreparedStatement - Effects of passing parameters by value vs reference in methods - Scenarios that throw checked exceptions - Requirements for thread synchronization calls like wait() - Outcomes of casting and inheritance between classes

Uploaded by

TRICKY MIND
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

1 Which of the following are uses of Object class?

(Choose 3)

Answer: a. to achieve inheritance at user-defined class level

b. to generate String representation of an object

c. to get the HashCode for an object

d. to achieve polymorphism at user-defined class level

e. to handle any Java Object in the name of Object

2 Which of the following options are true for StringBuffer class?(choose 3)

Answer: a. StringBuffer is extended from String class

b. StringBuffer is threadsafe

c. 'capacity' property indicates the maximum number of


characters that a StringBuffer can have

d. StringBuffer implements Charsequence interface

e. Buffer space in StringBuffer can be shared

3 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. Output of Line a and Line b will be same

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

c. Line a prints the corresponding classname with Object's


hashcode in Hexadecimal

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


with Object's hashcode in Hexa Decimal

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

4 Which of the following are true regarding PreparedStatement?(choose 3)

Answer: a. PreparedStatement are the SQL templates available in the


database server, that can be used directly without writing
complex SQL code

b. PreparedStatement cannot be used to create Scrollable


ResultSet

c. Parameters can be passed to PreparedStatement at run-time

d. PreparedStatements are precompiled SQL statements that


are faster in execution

e. PreparedStatement is the sub interface of Statement


interface

5 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 world

b. world world world

c. Hello Hello Hello

d. world Hello Hello

e. Hello world Hello

6 Consider the following code:

1. public class Circle1 {


2. private String string = "String1";
3. void work() {
4. String x = "String2";
5. class Circle2 {
6. public void peepOut() {
7. System.out.println(string);
8. System.out.println(x);
9. }
10. }
11. new Circle2().peepOut();
12. }
13.
14. public static void main(String args[]) {
15. Circle1 c1 = new Circle1();
16. c1.work();
17. }
18. }

Which of the following changes made to the above code will make the code to
compile and execute properly and gives the following output?

String1
String2

Answer: a. The variable at line 4 should be declared as final

b. The variable at line 2 should be declared as final

c. The method at line 6 should be defined as final method

d. The inner class Circle 2 should be an abstract class

e. The object for the inner class Circle2 should be created in


main() method

7 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. returns true if s2 is a subset of s1

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


s2

e. copies elements from s2 to s1

8 Consider the following scenario:

A Chat application written in Java, currently works with a general room facility,
where the messages posted by the logged user are displayed. A common
synchronized object is ued to Queue up the messages received from the users.

Now, the application needs additional feature called personal messaging, that
enables the user to make one-to-one communication.

Which of the following helps to implement the requirement?

Answer: a. A new synchronized object need to be created for every one-


to-one personal messaging request from the user

b. The personal messaging feature cannot be added when there


is a general room facility

c. A part of the synchronized object that already exists for the


general room can be used

d. Just two more threads need to be created at the user end

9 Under which of the following scenarios a checked exception is thrown? (Choose


2)

Answer: a. 5th element of an array is accessed, whose size is 3

b. A file that actually does not exist, is opened for reading


c. An attempt to connect to the database is made but failed.

d. length() method is called on a String object, that is assigned


to null

e. Given username and password is checked with database and


found invalid

10 Which of the following statement is true?

Answer: a. To call the sleep() method, a thread must own the lock of the
object which the call is to be made.

b. To call the join() method, a thread must own the lock of the
object on which the call is to be made

c. To call the wait() method, a thread must own the lock of the
object on which the call is to be made.

d. To call the yield() method, a thread must own the lock of the
object on which the call is to be made.

e. To call the wait() method, a thread must own the lock of the
current thread.

11 Consider the following code:

class Animal {
public String noise() { return "noise"; }
}

class Dog extends Animal {


public String noise() { return "bark"; }
}

class Cat extends Animal {


public String noise() { return "meow"; }
}
class MakeNoise {
public static void main(String args[]) {
Animal animal = new Dog();
Cat cat = (Cat)animal;
System.out.println(cat.noise());
}
}

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

Answer: a. noise

b. bark

c. meow

d. Compilation fails

e. An exception is thrown at runtime

12 Consider the following code:

public class GetArray {


public static void main(String args[]) {
float invt[][];
float[] prct, grts[];
float[][] sms, hms[];
(// Insert statement1 here)
(// Insert statement2 here)
(// Insert statement3 here)
}
}

Which of the following listed statements can be inserted at the above


commented lines
(// Insert statement1 here, // Insert statement2 here, // Insert statement3
here)
to make the program to compile without errors? (Choose 3)

Answer: a. grts = new float[1][4];

b. invt = grts;

c. hms = new float[2][5];

d. invt = new float[4][2];

e. grts = new float[1];

13 Consider the following code:

public class SwitchCase {


public static void main(String args[]) {
int x = 10;
switch(x) {
case 10: System.out.println("10");
case 10: System.out.println("10");
case 20: System.out.println("20");
default: System.out.println("30");
}
}
}

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

Answer: a. 10
10
20

b. 10
20

c. 10
10
d. Compilation Error

e. 30

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

Answer: a. Polymorphism

b. Abstraction

c. Inheritance

d. Reusability

15 Consider the following code snippet:

String deepak = "Did Deepak see bees? Deepak did.";

Which of the following options will give the output for the method call
deepak.charAt(10)?

Answer: a. None of the listed options

b. s

c. h

d. space

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 3.

c. Compile-time error at 4.

d. Compile-time error at 2.

e. Compile-time error at 1.

17 From a Collection object c, another Collection object needs to be created. It


should contain the same elements in the same order as that of source object c,
but with all duplicates eliminated.

Which of the following options provide the valid code to accomplish the above
given scenario?

Answer: a. new HashSet<Type>(c);


b. All of the listed options

c. new LinkedTreeSet<Type>(c);

d. new LinkedHashSet<Type>(c);

18 From JDK 1.6, which of the following interfaces is also implemented by


java.util.TreeSet class?

Answer: a. NavigableSet

b. NavigableList

c. NavigableMap

d. Deque

19 Which of the following modifier cannot be applied to the declaration of a field


(member of a class)?

Answer: a. public

b. final

c. abstract

d. private

e. protected

20 Which of the following statements are correct regarding Instance


Block?(Choose 3)
Answer: a. A class can have more than one instance block

b. An instance block cannot initialise the class members

c. Instance blocks are executed only when the instances are


created from main() method of that class

d. Instance blocks are executed before constructors

e. Instance blocks are executed for every created instance

21 Which of the following is the correct syntax for Annotation declaration?

Answer: a. interface author{


@String name(),
String date()
}

b. @interface author{
@String name();
@String date();
}

c. interface author{
String name(),
String date()
}

d. interface @author{
String name(),
String date()
}

e. @interface author{
String name();
String date();
}
22 Consider the following code:

class Resource { }
class UserThread extends Thread {
public Resource res;
public void run() {
try {
synchronized(res) {
System.out.println("Planet");
res.wait();
Thread.sleep(1000);
res.notify();
System.out.println("Earth");
}
} catch(InterruptedException e) { }
}
}

public class StartUserThreads {


public static void main(String[] args) {
Resource r = new Resource();
UserThread ut1 = new UserThread();
ut1.res = r;
UserThread ut2 = new UserThread();
ut2.res = r;
ut1.start();
ut2.start();
}
}

Which of the following will give the correct output for the above code?

Answer: a. Runtime Error "IllegalThreadStateException"

b. Compile-time Error

c. Prints the following output 2 times: Planet


(waits for 1000 milli seconds)
Earth
d. Prints the following output:
Planet
Planet
(get into long wait. Never ends)

e. Prints the following output 1 time: Planet


(waits for 1000 milli seconds)
Earth

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


Object and Cell Objects?

Answer: a. Polymorphism

b. Association

c. Inheritance

d. Aggregation

e. Persistence

24 Consider the following program:

class joy extends Exception { }


class smile extends joy { }
interface happy {
void a() throws smile;
void z() throws smile;
}

class one extends Exception { }


class two extends one { }
abstract class test {
public void a()throws one { }
public void b() throws one { }
}

public class check extends test {


public void a() throws smile {
System.out.println("welcome");
throw new smile();
}

public void b() throws one {


throw new two();
}

public void z() throws smile {


throw new smile();
}

public static void main(String args[]) {


try {
check obj=new check();
obj.b();
obj.a();
obj.z();
} catch(smile s) {
System.out.println(s);
}catch(two t) {
System.out.println(t.getClass());
}catch(one o) {
System.out.println(o);
}catch(Exception e) {
System.out.println(e);
}
}
}

What will be the output of the above program?

Answer: a. throws a compile time exception as overridden method a()


does not throw exception smile

b. welcome
class two
c. two

d. throws a compile time exception as overridden method z()


does not throw exception smile

e. class two

25 Consider the following code snippet:

public class TasteIt{


public void show() {
System.out.println("one");
}
public static void main(String[] args) {
TasteIt t = new TasteIt() {
public void show() {
System.out.println("two");
super.show();
}
};
t.show();
}
}

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

Answer: a. one
two

b. two
one

c. Compilation fails. Cannot use super keyword inside an


anonymous class

d. Runtime exception. Cannot find the super class version of


show() method
26 Consider the following code snippet:

public class Welcome {


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

public Welcome(int value) {


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

public static void main(String args[]) {


Welcome t = new Welcome();
System.out.println(t.title);
}
}

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

Answer: a. Welcome

b. Welcome Planet

c. Compilation fails

d. The code runs with no output

e. Welcome Planet 5

27 Consider the following program:

public class Exp3 {


public static void main(String[] args) {
try {
if (args.length == 0) return;
System.out.println(args[0]);
} finally {
System.out.println("The end");
}
}
}

Which of the following options are true regarding the output of the above
program? (Choose 2)

Answer: a. If run with no arguments, the program will print "The end"

b. If run with one argument, the program will print the given
argument followed by "The end"

c. If run with one argument, the program will simply print the
given argument

d. The program will throw an ArrayIndexOutOfBoundsException

e. If run with no arguments, the program will produce no


output

28 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. four

b. one

c. none of the objects are eligible

d. two

e. three

29 Consider the following scenario:

A given String needs to be searched, in text file, and report the number of
occurences with corresponding line numbers.

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

Answer: a. FileInputStream and PipedInputStream

b. FileInputStream and InputStreamReader

c. InputStreamReader and FilterInputStream

d. FileInputStream and SearchInputStream

e. FileReader and BufferedReader

30 Given the following method in an application:

1. public String setFileType( String fname ){


2. int p = fname.indexOf( '.' );
3. if( p > 0 ) fname = fname.substring( 0,p );
4. fname += ".TXT";
5. return fname;
6. }

and given that another part of the class has the following code

7. String TheFile = "Program.java";


8. File F = new File( setFileType( TheFile ) );
9. System.out.println( "Created " + TheFile );

Which of the following will be the output for the statement in line 9?

Answer: a. Created Program.txt

b. Created Program

c. Created Program.java

d. Created Program.java.txt

31 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 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.

b. 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

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

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

e. Reads the text from keyboard character by character and


prints the same to the console on typing every character.

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

Answer: a. A program can suggest that garbage collection be performed


but not force it

b. An object becomes eligible for garbage collection when all


references denoting it are set to null

c. None of the listed options

d. Garbage collection is platform independent

e. The automatic garbage collection of the JVM prevents


programs from ever running out of memory

33 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. Compile-time error: Integer wrapper cannot accept char type

b. Prints: EQ

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


using equals

d. Prints: equals EQ

e. Prints: equals

34 Consider a List based object L, with size of 10 elements e, and the following two
lines of code:
L.add("e");
L.remove("e");

Which of the following options gives the status about the List object L after
executing the above two lines of code?

Answer: a. New elements are added and old ones are taken out but size
is increasing
b. New elements are added and old ones are taken out but
there will be a change in size

c. No change because we are adding and deleting the same


element

d. New elements are added and old ones are taken out but no
change in size

e. New elements can be added but cannot be removed

35 Consider the following code:

class A {
public void method(Object object) {
System.out.println("Object");
}
public void method(String string) {
System.out.println("String");
}
public static void main(String args[]) {
new A().method(null);
}
}
Which of the following options will be the output for the above code?

Answer: a. Compilation Error 'Cannot pass null as method arguments'

b. Prints: String

c. Throws NullPointerException at runtime

d. Prints: Object

36 Consider the following code snippet:

class MyClass {
int myValue;

@Override
public boolean equals(Object o1, Object o2){
MyClass mc1=(MyClass) o1;
MyClass mc1=(MyClass) o2;
if(mc1.myValue==mc2.myValue)
return true;
return false;
}
}

what is the correct output of the above code snippet?

Answer: a. ClassCastExceprion will be thrown.

b. @Override cannot be used for equals() method.

c. Compile error: class doesn't override a method from it's


superclass @Override.

d. Program compiles sucessfully and executes.

37 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. The code is wrong. Exceptions should be caught in reversed


hierarchy order.

b. Line 1 : throw new Exception();


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

c. Line 1 : throw new IOException();


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

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();

38 Consider the following code:

class MyThread extends Thread {


MyThread() {
System.out.print(" MyThread");
}
public void run() { System.out.print(" queen"); }
public void run(String s) { System.out.print(" jack"); }
}

public class TestThreads {


public static void main (String [] args) {
Thread t = new MyThread() {
public void run() { System.out.print(" king"); }
};
t.start();
}
}

Which of the followingl gives the correct valid output for the above code?

Answer: a. queen king

b. king queen

c. Compilation fails.

d. MyThread king

e. MyThread queen

39 Which of the following are interfaces in JDBC API?(choose 3)

Answer: a. SQLWarning

b. Statement

c. Connection

d. DriverManager

e. CallableStatement
40 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. go();

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

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

d. new Bar().go();

e. new Choco().go();

41 Consider the following code:

public class Code17 {


public static void main(String args[]) {
new Code17();
}
{
System.out.print("Planet ");
}
{
System.out.print("Welcome ");
}
}
Which of the following will be the valid output for the above code?

Answer: a. Compiles and Executes with no output

b. Welcome Planet

c. Planet Welcome

d. Planet

e. Compilation Error

42 Consider the following code:

1. class Test {
2. public static void main(String args[]) {
3. double d = 12.3;
4. Dec dec = new Dec();
5. dec.dec(d);
6. System.out.println(d);
7. }
8. }
9. class Dec{
10. public void dec(double d) { d = d - 2.0d; }
11. }

Which of the following gives the correct value printed at line 6?

Answer: a. Prints: -2.0


b. Prints: 10.3

c. Prints: 12.3

d. Prints: 0.0

43 All kinds of looping constructs designed using while loop can also be
constructed using do-while loop.
State True or False.

Answer: True False

44 ResultSet programming is more efficient, where there are frequent insertions,


updations and deletions. State True or False.

Answer: True False

45 Consider the following code snippet:

import java.util.*;
import java.text.*;

public class TestCol5 {


public static void main(String[] args) {
String dob = "17/03/1981";
// Insert Code here
}
}

Which of the following code snippets, when substituted to (//Insert Code here)
in the above program, will convert the dob from String to Date type?
Answer: a. DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
try {
Date d = df.parse(dob);
} catch(ParseException pe) { }

b. GregorianCalendar g = new GregorianCalendar(dob);

Date d = g.getDate();

c. Date d = new Date(dob);

d. CalendarFormat cf = new
SimpleCalendarFormat("dd/MM/yyyy");
try {
Date d = cf.parse(dob);
} catch(ParseException pe) { }

e. Calendar c = new Calendar(dob);


Date d = g.getDate();

You might also like