SCJP Certification Questions With Answers Explanation :Java(Set-13)

Q. Given:

5. public class Tahiti {

6. Tahiti t;

7. public static void main(String[] args) {

8. Tahiti t = new Tahiti();

9. Tahiti t2 = t.go(t);

10. t2 = null;

11. // more code here

12. }

13. Tahiti go(Tahiti t) {

14. Tahiti t1 = new Tahiti(); Tahiti t2 = new Tahiti();

15. t1.t = t2; t2.t = t1; t.t = t2;

16. return t1;

17. }

18. }

When line 11 is reached, how many objects are eligible for garbage collection?

A. 0

B. 1

C. 2

D. 3

E. Compilation fails.

Answer: A

—————————————————————————————-

Q. Given:

11. public class Commander {

12. public static void main(String[] args) {

13. String myProp = /* insert code here */

14. System.out.println(myProp);

15. }

16. }

and the command line: java -Dprop.custom=gobstopper Commander Which two, placed

on line 13, will produce the output gobstopper? (Choose two.)

A. System.load(“prop.custom”);

B. System.getenv(“prop.custom”);

C. System.property(“prop.custom”);

D. System.getProperty(“prop.custom”);

E. System.getProperties().getProperty(“prop.custom”);

Answer: D,E

Explanation :We are changing value of prop.custom to gobstopper at runtime using command line

—————————————————————————————-

Q. Given:

11. public class ItemTest {

12. private final int id;

13. public ItemTest(int id) { this.id = id; }

14. public void updateId(int newId) { id = newId; }

15.

16. public static void main(String[] args) {

17. ItemTest fa = new ItemTest(42);

18. fa.updateId(69);

19. System.out.println(fa.id);

20. }

21. }

What is the result?

A. Compilation fails.

B. An exception is thrown at runtime.

C. The attribute id in the ItemTest object remains unchanged.

D. The attribute id in the ItemTest object is modified to the new value.

E. A new ItemTest object is created with the preferred value in the id attribute.

Answer: A

Explanation :Compilation will be fail as we can’t change final variable after assigning value to that once.

—————————————————————————————-

Q. A developer is creating a class Book, that needs to access class Paper. The Paper class is

deployed in a JAR named myLib.jar. Which three, taken independently, will allow the

developer to use the Paper class while compiling the Book class? (Choose three.)

A. The JAR file is located at $JAVA_HOME/jre/classes/myLib.jar.

B. The JAR file is located at $JAVA_HOME/jre/lib/ext/myLib.jar..

C. The JAR file is located at /foo/myLib.jar and a classpath environment variable is set

that includes /foo/myLib.jar/Paper.class.

D. The JAR file is located at /foo/myLib.jar and a classpath environment variable is set

that includes /foo/myLib.jar.

E. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -

cp /foo/myLib.jar/Paper Book.java.

F. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -d

/foo/myLib.jar Book.java

G. The JAR file is located at /foo/myLib.jar and the Book class is compiled using javac -

classpath /foo/myLib.jar Book.java

Answer: B,D,G

—————————————————————————————-

Q. Given:

15. public class Yippee {

16. public static void main(String [] args) {

17. for(int x = 1; x < args.length; x++) {

18. System.out.print(args[x] + ” “);

19. }

20. }

21. }

and two separate command line invocations: java Yippee java Yippee 1 2 3 4 What is the

result?

A. No output is produced. 1 2 3

B. No output is produced. 2 3 4

C. No output is produced. 1 2 3 4

D. An exception is thrown at runtime. 1 2 3

E. An exception is thrown at runtime. 2 3 4

F. An exception is thrown at runtime. 1 2 3 4

Answer: B

Explanation :For first runing command will not produce result as no command line arguments we have passed.Second case will produce output as 234 we are accessing value from asgs[1] position.

—————————————————————————————-

Q. Given classes defined in two different files:

1. package packageA;

2. public class Message {

3. String getText() { return “text”; }

4. }

And:

1. package packageB;

2. public class XMLMessage extends packageA.Message {

3. String getText() { return “<msg>text</msg>”;}

4. public static void main(String[] args) {

5. System.out.println(new XMLMessage().getText());

6. }

7. }

What is the result of executing XMLMessage.main?

A. text

B. Compilation fails.

C. <msg>text</msg>

D. An exception is thrown at runtime.

Answer: B

Explanation :Here we are using wrong syntax to access pakageA class.

—————————————————————————————-

Q. Given:

3. interface Fish { }

4. class Perch implements Fish { }

5. class Walleye extends Perch { }

6. class Bluegill { }

7. public class Fisherman {

8. public static void main(String[] args) {

9. Fish f = new Walleye();

10. Walleye w = new Walleye();

11. Bluegill b = new Bluegill();

12. if(f instanceof Perch) System.out.print(“f-p “);

13. if(w instanceof Fish) System.out.print(“w-f “);

14. if(b instanceof Fish) System.out.print(“b-f “);

15. }

16. }

What is the result?

A. w-f

B. f-p w-f

C. w-f b-f

D. f-p w-f b-f

E. Compilation fails.

F. An exception is thrown at runtime.

Answer: B

Explanation :We use instanceof operator to check the objects status like f is object-of Perch.

—————————————————————————————-

Q. Given:

1. package com.company.application;

2.

3. public class MainClass {

4. public static void main(String[] args) {}

5. }

And MainClass exists in the /apps/com/company/application directory. Assume the

CLASSPATH environment variable is set to “.” (current directory).Which two java

commands entered at the command line will run MainClass? (Choose two.)

A. java MainClass if run from the /apps directory

B. java com.company.application.MainClass if run from the /apps directory

C. java -classpath /apps com.company.application.MainClass if run from any directory

D. java -classpath . MainClass if run from the /apps/com/company/application directory

E. java -classpath /apps/com/company/application:. MainClass if run from the /apps

directory

F. java com.company.application.MainClass if run from the

/apps/com/company/application directory

Answer: B,C

—————————————————————————————-

Q. Given that the current directory is empty, and that the user has read and write privileges

to the current directory, and the following:

1. import java.io.*;

2. public class Maker {

3. public static void main(String[] args) {

4. File dir = new File(“dir”);

5. File f = new File(dir, “f”);

6. }

7. }

Which statement is true?

A. Compilation fails.

B. Nothing is added to the file system.

C. Only a new file is created on the file system.

D. Only a new directory is created on the file system.

E. Both a new file and a new directory are created on the file system.

Answer: B

Explanation :Here we are only creating file object but actually not making dir or file.

—————————————————————————————-

Q. Given:

12. NumberFormat nf = NumberFormat.getInstance();

13. nf.setMaximumFractionDigits(4);

14. nf.setMinimumFractionDigits(2);

15. String a = nf.format(3.1415926);

16. String b = nf.format(2);

Which two statements are true about the result if the default locale is Locale.US?

(Choose two.)

A. The value of b is 2.

B. The value of a is 3.14.

C. The value of b is 2.00.

D. The value of a is 3.141.

E. The value of a is 3.1415.

F. The value of a is 3.1416.

G. The value of b is 2.0000.

Answer: C,F

—————————————————————————————-

Q. Which three statements concerning the use of the java.io.Serializable interface are true?

(Choose three.)

A. Objects from classes that use aggregation cannot be serialized.

B. An object serialized on one JVM can be successfully deserialized on a different JVM.

C. The values in fields with the volatile modifier will NOT survive serialization and deserialization.

D. The values in fields with the transient modifier will NOT survive serialization and deserialization.

E. It is legal to serialize an object of a type that has a supertype that does NOT implement java.io.Serializable.

Answer: B,D,E

—————————————————————————————-

Q. Given:

12. String csv = “Sue,5,true,3″;

13. Scanner scanner = new Scanner( csv );

14. scanner.useDelimiter(“,”);

15. int age = scanner.nextInt();

What is the result?

A. Compilation fails.

B. After line 15, the value of age is 5.

C. After line 15, the value of age is 3.

D. An exception is thrown at runtime.

Answer: D

Explanation :We have to use try/catch block when using scanner class otherwise it will throw error.

—————————————————————————————-

Q. Given that c is a reference to a valid java.io.Console object, which two code fragments

read a line of text from the console? (Choose two.)

A. String s = c.readLine();

B. char[] c = c.readLine();

C. String s = c.readConsole();

D. char[] c = c.readConsole();

E. String s = c.readLine(“%s”, “name “);

F. char[] c = c.readLine(“%s”, “name “);

Answer: A,E

—————————————————————————————-

Q. Given:

11. String test = “a1b2c3″;

12. String[] tokens = test.split(“\\d”);

13. for(String s: tokens) System.out.print(s + ” “);

What is the result?

A. a b c

B. 1 2 3

C. a1b2c3

D. a1 b2 c3

E. Compilation fails.

F. The code runs with no output.

G. An exception is thrown at runtime.

Answer: A

Explanation :We are spliting string on the basic of integer value.

—————————————————————————————-

Q. Given:

1. public class KungFu {

2. public static void main(String[] args) {

3. Integer x = 400;

4. Integer y = x;

5. x++;

6. StringBuilder sb1 = new StringBuilder(“123″);

7. StringBuilder sb2 = sb1;

8. sb1.append(“5″);

9. System.out.println((x==y) + ” ” + (sb1==sb2));

10. }

11. }

What is the result?

A. true true

B. false true

C. true false

D. false false

E. Compilation fails.

F. An exception is thrown at runtime.

Answer: B

Explantion :When we use == with obects refrence then it will compare refrence variable value otherwise it will compare actual value inside variables.

—————————————————————————————-

Q. Given:

11. class Converter {

12. public static void main(String[] args) {

13. Integer i = args[0];

14. int j = 12;

15. System.out.println(“It is ” + (j==i) + ” that j==i.”);

16. }

17. }

What is the result when the programmer attempts to compile the code and run it with the

command java Converter 12?

A. It is true that j==i.

B. It is false that j==i.

C. An exception is thrown at runtime.

D. Compilation fails because of an error in line 13.

Answer: D

Explanation :No command line argument present.

—————————————————————————————-

Q. Given

10. class Foo {

11. static void alpha() { /* more code here */ }

12. void beta() { /* more code here */ }

13. }

Which two statements are true? (Choose two.)

A. Foo.beta() is a valid invocation of beta().

B. Foo.alpha() is a valid invocation of alpha().

C. Method beta() can directly call method alpha().

D. Method alpha() can directly call method beta().

Answer: B,C

Explanation :We can call static method by using class name.And static mehtod can be called directly in same class.

—————————————————————————————-

Q. Which two code fragments correctly create and initialize a static array of int elements?

(Choose two.)

A. static final int[] a = { 100,200 };

B. static final int[] a; static { a=new int[2]; a[0]=100; a[1]=200; }

C. static final int[] a = new int[2]{ 100,200 };

D. static final int[] a; static void init() { a = new int[3]; a[0]=100; a[1]=200; }

Answer: A,B

—————————————————————————————-

Q. Given:

5. class Atom {

6. Atom() { System.out.print(“atom “); }

7. }

8. class Rock extends Atom {

9. Rock(String type) { System.out.print(type); }

10. }

11. public class Mountain extends Rock {

12. Mountain() {

13. super(“granite “);

14. new Rock(“granite “);

15. }

16. public static void main(String[] a) { new Mountain(); }

17. }

What is the result?

A. Compilation fails.

B. atom granite

C. granite granite

D. atom granite granite

E. An exception is thrown at runtime.

F. atom granite atom granite

Answer: F

—————————————————————————————-

Q. Given:

1. public class Plant {

2. private String name;

3. public Plant(String name) { this.name = name; }

4. public String getName() { return name; }

5. }

1. public class Tree extends Plant {

2. public void growFruit() { }

3. public void dropLeaves() { }

4. }

Which statement is true?

A. The code will compile without changes.

B. The code will compile if public Tree() { Plant(); } is added to the Tree class.

C. The code will compile if public Plant() { Tree(); } is added to the Plant class.

D. The code will compile if public Plant() { this(“fern”); } is added to the Plant class.

E. The code will compile if public Plant() { Plant(“fern”); } is added to the Plant class.

Answer: D

—————————————————————————————-

This entry was posted in Java Certifications, Java Programing, Programing and tagged , , . Bookmark the permalink.

3 Responses to SCJP Certification Questions With Answers Explanation :Java(Set-13)

  1. Luka says:

    The example: public class XMLMessage extends packageA.Message doesn’t compile because in the superclass getText() has no modifier so only classes from the same package can override it! Your answer is incorrect because giving the full route to a class instead of importing it is legitimate way of doing it… Please edit your answer ;) Thanks

    • salil says:

      that example is compiling fine no errors ….. answer is C

    • Aman S. Aneja says:

      The XMLMessage class will compile just fine, because as the getText() method has no modifer in the Super Class (Message). The XMLMessage class doesn’t even know about any such method! So it will act as any other method in the XMLMessage class.

      So, on executing the code output will be:
      text

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>