Showing posts with label SCJP 6 Questions. Show all posts
Showing posts with label SCJP 6 Questions. Show all posts

Wednesday, February 13, 2013

SCJP Question: "hidding" issue

Given:

31. class Foo {
32. public int a = 3;
33. public void addFive() { a += 5; System.out.print("f "); }
34. }
35. class Bar extends Foo {
36. public int a = 8;
37. public void addFive() { this.a += 5; System.out.print("b " ); }
38. } Invoked with: Foo f = new Bar(); f.addFive(); System.out.println(f.a);


What is the result?
A. b 3
B. b 8
C. b 13
D. f 3
E. f 8
F. f 13
G. Compilation fails.
H. An exception is thrown at runtime.

SCJP Question with Garbage collection

Q. Given:

3. interface Animal { void makeNoise(); }
4. class Horse implements Animal {
5. Long weight = 1200L;
6. public void makeNoise() { System.out.println(“whinny”); }
7. }

8. public class Icelandic extends Horse {
9. public void makeNoise() { System.out.println(“vinny”); }
10. public static void main(String[] args) {
11. Icelandic i1 = new Icelandic();
12. Icelandic i2 = new Icelandic();
13. Icelandic i3 = new Icelandic();
14. i3 = i1; i1 = i2; i2 = null; i3 = i1;
15. }
16. }


When line 15 is reached, how many objects are eligible for the garbage collector?

A. 0
B. 1
C. 2
D. 3
E. 4
F. 6

Tuesday, February 12, 2013

SCJP: switch statement

A trick usual to be found concerns the type of the switch expression.
Only int convertibles or enums can be used in the switch expression.
long values fails to compile.

Wrong:

long l=0;
switch(l){
//...
}

SCJP Question: postfix increment

What is the output of this code?

class A {
  int i=0;

  public int getValue(){
    return i++;
  }
}

class Test {
  public static void main ( String args [ ] ) {
    System.out.println(new A().getValue());
  }
}


Monday, February 11, 2013

SCJP finalize question

Which statement is true?
A. A class's finalize() method CANNOT be invoked explicitly.
B. super.finalize() is called implicitly by any overriding finalize() method.
C. The finalize() method for a given object is called no more than once by the garbage collector.
D. The order in which finalize() is called on two objects is based on the order in which the two objects became finalizable.


Sunday, February 10, 2013

SCJP question

What is the result of this code?


class Person{
  String name="No name";
  public Person(String nm){ name=nm;}
}

class Employee extends Person {
  String empID="0000";
  public Employee(String id) { empID=id;}
}

public class EmployeeTest{
  public static void main(String [] args){
    Employee e=new Employee("4321");
    System.out.println(e.empID);
  }
}


A. 4321
B. 0000
C. An exception is thrown at Runtime
D. Compilation fails

Wednesday, February 6, 2013

SCJP 6 doubts: implicit imports

There are implicit imports that do not need to be declared.
The classes in java.lang package can be used without importing any package, this is an implicit import, so don't be fooled if you see a question that looks like that the import is missing.

What classes can we see in the java.lang package?
JavaSE 6 API just tells us which ones belong to this package: basically its the String, StringBuilder, StringBuffer, Thread, wrappers (Integer, Float,...), Object, System, Runtime, Exception, and a lot of other classes which you can use directly.

The real exam always has line numbers on the left side of the code, so if you see that it does not start with line 1, you don't have to worry about the imports, and assume that the imports (and packages) are right.
If the code starts at line 1, just assume you're looking at the entire file. If you see that the code uses classes that belong to other packages besides java.lang, (e.g class File needs import java.io.*) then you can say for sure that the right answer is "compilation fails" due to missing imports.

Default package
If you have a class within the default package (no package declaration is found), and you create another class that has a relashionship with the first one, then no import is needed (because both are in the default package);

1. public class A{
2. int i=0;
3. }

1. public class B extends A{
2. }

Monday, December 17, 2012

Whizlabs SCJP 6.0 Simulator "Error parsing question"

If you get an error message at the Whizlabs SCJP 6.0 Simulator saying "Error parsing question" at the start of an exame, ignore it and take the exame again.

If at the end of the exam, you have a message box that does not allow you to check you score, ignore it and go to the "Performance History". From there, you are able to check your score and review your questions.

Sunday, December 9, 2012

SCJP 6 Questions - Question 63

 class C {  
   public static void main ( String ka [ ] ) {  
     while ( false ) {  
       System.out.println ( "Value" ) ;  
     }  
   }  
 }  

What is the output?
1. compile time error
2. prints Value infinitely
3. Runtime Exception
4. None of the above