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

Q. Given:

1. class TestException extends Exception { }

2. class A {

3. public String sayHello(String name) throws TestException {

4. if(name == null) throw new TestException();

5. return “Hello ” + name;

6. }

7. }

8. public class TestA {

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

10. new A().sayHello(“Aiko”);

11. }

12. }

Which statement is true?

A. Compilation succeeds.

B. Class A does not compile.

C. The method declared on line 9 cannot be modified to throw TestException.

D. TestA compiles if line 10 is enclosed in a try/catch block that catches TestException.

Answer: D

Explanation :When we call a method which throw any exception then we have to handle or declare that exception in calling method.

Here in main method we call sayHello method whic throw exception so we have to put this code in try/catch block to compile code.

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

Q. Given:

11. public static Collection get() {

12. Collection sorted = new LinkedList();

13. sorted.add(“B”); sorted.add(“C”); sorted.add(“A”);

14. return sorted;

15. }

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

17. for (Object obj: get()) {

18. System.out.print(obj + “, “);

19. }

20. }

What is the result?

A. A, B, C,

B. B, C, A,

C. Compilation fails.

D. The code runs with no output.

E. An exception is thrown at runtime.

Answer: B

Explanation :This program execute normally here we are only adding string objects to collection and then printing these string using for loop.

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

Q. Given:

11. static class A {

12. void process() throws Exception { throw new Exception(); }

13. }

14. static class B extends A {

15. void process() { System.out.println(“B”); }

16. }

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

18. new B().process();

19. }

What is the result?

A. B

B. The code runs with no output.

C. Compilation fails because of an error in line 12.

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

E. Compilation fails because of an error in line 18.

Answer: A

Explanation :subclass process() method cannot throw more than Exception.Here this is okay as process method in derived class is not thowing any exception.So it work fine.

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

Q. Given:

10. public class Foo {

11. static int[] a;

12. static { a[0]=2; }

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

14. }

Which exception or error will be thrown when a programmer attempts to run this code?

A. java.lang.StackOverflowError

B. java.lang.IllegalStateException

C. java.lang.ExceptionInInitializerError

D. java.lang.ArrayIndexOutOfBoundsException

Answer: C

Explanation :Signals that an unexpected exception has occurred in a static initializer. An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.

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

Q. Given:

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

12. Integer i = new Integer(1) + new Integer(2);

13. switch(i) {

14. case 3: System.out.println(“three”); break;

15. default: System.out.println(“other”); break;

16. }

17. }

What is the result?

A. three

B. other

C. An exception is thrown at runtime.

D. Compilation fails because of an error on line 12.

E. Compilation fails because of an error on line 13.

F. Compilation fails because of an error on line 15.

Answer: A

Explanation :In Java 6 we have autoboxing facilite So when we used this line

Integer i = new Integer(1) + new Integer(2);

then these objects automatically become int value and add work.

Int i=3 ;

—————————————————————————————-
Q. Given:

11. public static Iterator reverse(List list) {

12. Collections.reverse(list);

13. return list.iterator();

14. }

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

16. List list = new ArrayList();

17. list.add(“1″); list.add(“2″); list.add(“3″);

18. for (Object obj: reverse(list))

19. System.out.print(obj + “, “);

20. }

What is the result?

A. 3, 2, 1,

B. 1, 2, 3,

C. Compilation fails.

D. The code runs with no output.

E. An exception is thrown at runtime.

Answer: C

Explanation :The for-each loop works with Iterable, not Iterator. There’s a difference. Make the reverse method return List (and so “return list” instead of “return list.iterator()”) and that code should work, since List extends Iterable (indirectly).

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

Q. Given:

1. public class TestString3 {

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

3. // insert code here

5. System.out.println(s);

6. }

7. }

Which two code fragments, inserted independently at line 3, generate the output 4247?

(Choose two.)

A. String s = “123456789″;

s = (s-”123″).replace(1,3,”24″) – “89″;

B. StringBuffer s = new StringBuffer(“123456789″);

s.delete(0,3).replace(1,3,”24″).delete(4,6);

C. StringBuffer s = new StringBuffer(“123456789″);

s.substring(3,6).delete(1,3).insert(1, “24″);

D. StringBuilder s = new StringBuilder(“123456789″);

s.substring(3,6).delete(1,2).insert(1, “24″);

E. StringBuilder s = new StringBuilder(“123456789″);

s.delete(0,3).delete(1,3).delete(2,5).insert(1, “24″);

Answer: B,E

Explanation :StringBuffer and StringBuilder does not create new string when we add or modify existing string objects.

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

Q. Given:

1. d is a valid, non-null Date object

2. df is a valid, non-null DateFormat object set to the current locale

What outputs the current locale’s country name and the appropriate version of d’s date?

A. Locale loc = Locale.getLocale();

System.out.println(loc.getDisplayCountry()

+ ” ” + df.format(d));

B. Locale loc = Locale.getDefault();

System.out.println(loc.getDisplayCountry()

+ ” ” + df.format(d));

C. Locale loc = Locale.getLocale();

System.out.println(loc.getDisplayCountry()

+ ” ” + df.setDateFormat(d));

D. Locale loc = Locale.getDefault();

System.out.println(loc.getDisplayCountry()

+ ” ” + df.setDateFormat(d));

Answer: B

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

Q.

5. import java.util.Date;

6. import java.text.DateFormat;

21. DateFormat df;

22. Date date = new Date();

23. // insert code here

24. String s = df.format(date);

A. df = new DateFormat();

B. df = Date.getFormat();

C. df = date.getFormat();

D. df = DateFormat.getFormat();

E. df = DateFormat.getInstance();

Answer: E

Explanation :Firstly we have to make object of dateformat class.

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

Q. Given:

1. public class BuildStuff {

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

3. Boolean test = new Boolean(true);

4. Integer x = 343;

5. Integer y = new BuildStuff().go(test, x);

6. System.out.println(y);

7. }

8. int go(Boolean b, int i) {

9. if(b) return (i/7);

10. return (i/49);

11. }

12. }

What is the result?

A. 7

B. 49

C. 343

D. Compilation fails.

E. An exception is thrown at runtime.

Answer: B

Explanation :When if condition will be true then it will return value i/7.

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

Q. Given:

12. import java.io.*;

13. public class Forest implements Serializable {

14. private Tree tree = new Tree();

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

16. Forest f = new Forest();

17. try {

18. FileOutputStream fs = new FileOutputStream(“Forest.ser”);

19. ObjectOutputStream os = new ObjectOutputStream(fs);

20. os.writeObject(f); os.close();

21. } catch (Exception ex) { ex.printStackTrace(); }

22. } }

23.

24. class Tree { }

What is the result?

A. Compilation fails.

B. An exception is thrown at runtime.

C. An instance of Forest is serialized.

D. An instance of Forest and an instance of Tree are both serialized.

Answer: B

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

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

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>