Monday, January 3, 2011

Self Test – Chapters 1 to 5

We have successfully completed 5 chapters that have covered the basics of the java programming language (Of course, there is still a lot more basic stuff to come) Let us now try to assess how much knowledge we have gained in these chapters.

The following is a quick self-assessment (test). All questions are multiple choice (Like the original SCJP exam). Try to answer them based on what you have learnt in these previous 5 chapters. Don’t worry, I have given the answers to the questions too at the end of the chapter to let you judge how well you have learnt these topics.

Questions:

Question 1:

Which is true? (Choose all that apply.)
A. “X extends Y” is correct if and only if X is a class and Y is an interface
B. “X extends Y” is correct if and only if X is an interface and Y is a class
C. “X extends Y” is correct if X and Y are either both classes or both interfaces
D. “X extends Y” is correct for all combinations of X and Y being classes and/or interfaces

Question 2:

Which method names follow the JavaBeans standard? (Choose all that apply.)
A. addSize
B. getCust
C. deleteRep
D. isTrue
E. putDimensions

Question 3:

Which of the following are valid method declarations? Choose all that apply.
A. public abstract final String getName();
B. public final getName() { return “Anand”; }
C. public static void doSomething() {…}
D. public abstract String getName() { return “Anand” };

Question 4:

Given:
1. enum Animals {
2. DOG("woof"), CAT("meow"), FISH("burble");
3. String sound;
4. Animals(String s) { sound = s; }
5. }
6. class TestEnum {
7. static Animals a;
8. public static void main(String[] args) {
9. System.out.println(a.DOG.sound + " " + a.FISH.sound);
10. }
11. }

What is the result?

A. woof burble
B. Multiple compilation errors
C. Compilation fails due to an error on line 2
D. Compilation fails due to an error on line 3
E. Compilation fails due to an error on line 4
F. Compilation fails due to an error on line 9

Question 5:

Given:

1. public class Electronic implements Device
{ public void doIt() { } }
2.
3. abstract class Phone1 extends Electronic { }
4.
5. abstract class Phone2 extends Electronic
{ public void doIt(int x) { } }
6.
7. class Phone3 extends Electronic implements Device
{ public void doStuff() { } }
8.
9. interface Device { public void doIt(); }

What is the result? (Choose all that apply.)

A. Compilation succeeds
B. Compilation fails with an error on line 1
C. Compilation fails with an error on line 3
D. Compilation fails with an error on line 5
E. Compilation fails with an error on line 7
F. Compilation fails with an error on line 9

Question 6:

Given:

4. class Announce {
5. public static void main(String[] args) {
6. for(int __x = 0; __x < 3; __x++) ; 7. int #lb = 7; 8. long [] x [5]; 9. Boolean []ba[]; 10. enum Traffic { RED, YELLOW, GREEN }; 11. } 12. } What is the result? (Choose all that apply.) A. Compilation succeeds B. Compilation fails with an error on line 6 C. Compilation fails with an error on line 7 D. Compilation fails with an error on line 8 E. Compilation fails with an error on line 9 F. Compilation fails with an error on line 10 Question 7:

Given:

3. public class TestDays {
4. public enum Days { MON, TUE, WED };
5. public static void main(String[] args) {
6. for(Days d : Days.values() )
7. ;
8. Days [] d2 = Days.values();
9. System.out.println(d2[2]);
10. }
11. }

What is the result? (Choose all that apply.)

A. TUE
B. WED
C. The output is unpredictable
D. Compilation fails due to an error on line 4
E. Compilation fails due to an error on line 6
F. Compilation fails due to an error on line 8
G. Compilation fails due to an error on line 9

Question 8:

Given:

4. public class Frodo extends Hobbit {
5. public static void main(String[] args) {
6. Short myGold = 7;
7. System.out.println(countGold(myGold, 6));
8. }
9. }
10. class Hobbit {
11. int countGold(int x, int y) { return x + y; }
12. }

What is the result?

A. 13
B. Compilation fails due to multiple errors
C. Compilation fails due to an error on line 6
D. Compilation fails due to an error on line 7
E. Compilation fails due to an error on line 11

Question 9:

Which of the following statements are true? Choose all that apply

A. An abstract class can have final methods
B. A Final class can have abstract methods
C. A static method can access instance variables
D. An abstract class can have one or more methods that contain implementations

Question 10:

Given the code below, Guess the output:

1. public class test {
2. public String name = “Anand”;
3. public String getName() {
4. return this.name;
5. }
6. public static void main(String[] args) {
7. System.out.print (“Name is:” + name);
8. System.out.print(“Name is:” + getName());
9. }
10. }

A. Output Anand Anand gets printed in the console
B. Compilation error at line 7
C. Compilation error at line 8
D. Program compiles fine but fails at runtime

Answers:

Answer 1:

C is correct

A is incorrect because classes implement interfaces, they don’t extend them. B is incorrect because interfaces only “inherit from” other interfaces. D is incorrect based on the preceding rules.

Answer 2:

B and D use the valid prefixes ‘get’ and ‘is’. And hence they are correct

A is incorrect because ‘add’ can be used only with Listener methods. C and E are incorrect because ‘delete’ and ‘put’ are not standard JavaBeans name prefixes.

Answer 3:

C is a valid method declaration and hence is correct.

A is incorrect because a method cannot be both abstract and final. B is incorrect because the method’s return type “String” is missing. D is incorrect because an abstract method cannot have any code inside it.

Answer 4:

A is correct; enums can have constructors and variables.

B, C, D, E, and F are incorrect; these lines all use correct syntax

Answer 5:

A is correct; all of these are legal declarations.

B, C, D, E, and F are incorrect based on the above information

Answer 6:

C, D, and F are correct. Variable names cannot begin with a #, an array declaration can’t include a size without an instantiation, and enums can’t be declared within a method.

A, B, and E are incorrect based on the above information

Answer 7:

B is correct. Every enum comes with a static values() method that returns an array of the enum’s values, in the order in which they are declared in the enum

A, C, D, E, F, and G are incorrect based on the above information

Answer 8:

D is correct. The Short myGold is autoboxed correctly, but the countGold() method cannot be invoked from a static context

A, B, C, and E are incorrect based on the above information

Answer 9:

D is correct because an abstract class can have one or more methods that contain implementations (code)
A is correct because an abstract class can have final methods

B is incorrect because a final class cannot have abstract methods C is incorrect because a static method cannot access instance variables

Answer 10:

B & C are correct because the main method is static and it cannot access instance methods or variables.

A & D are incorrect because of the above mentioned reasons.

Previous Chapter - Quick Recap - Chapters 1 to 5

Next Chapter - Chapter 6: Object Oriented Concepts - Encapsulation

12 comments:

  1. Can you post some more questions for the scjp exam?

    ReplyDelete
  2. @ Rajesh

    Yes Rajesh that is in my plan. you shall see it soon.

    ReplyDelete
  3. Hi Anand ,
    Thanx for posting...
    I think an abstract class can have final methods.please clarify my doubt

    ReplyDelete
  4. @ MAD'u CrY

    Yes, an abstract class can have final methods. An abstract class can have any number of non-abstract methods and a final method too can be one of those non abstract methods.

    not a problem at all

    Anand.

    ReplyDelete
  5. If an abstract class can have final method then why Q#9 A is incorrect??

    ReplyDelete
  6. @ Fahad

    An abstract class can have methods with code but cannot have final methods. So option D is correct and others are wrong.

    Anand

    ReplyDelete
  7. Hi,

    Regarding Q#9,

    An Abstract Class can Have Final Methods
    hence for that option A is also correct Right?

    ReplyDelete
    Replies
    1. @ Anonymous,
      Yes, Options A & D are correct for this Q9.

      Anand

      Delete
  8. Hi Anand,

    Abstract class can have final methods but it doesn't make sense.

    To make it usable, we can have static final methods inside an abstract class.
    FYI, I just tested it. It works.

    ReplyDelete
    Replies
    1. Hi Anonymous,
      The purpose of an Abstract class is to promote re-use where your child classes may use some default behavior that you have defined while they have freedom to implement their own features in their classes. If you feel that some methods must not be messed with by your child classes, you can make that method final. This does not alter the behavior of the parent abstract class in any way. The child just wont be able override/implement the final method again inside their bounds - thats all.

      Good Try - For the static final methods inside an abstract class. Glad to see you are experimenting.

      Anand

      Delete
    2. Right Anand,

      So,we can conclude that AN ABSTRACT CLASS CAN HAVE FINAL METHODS WHICH ARE NOT ABSTRACT (final methods can never be abstract) i.e. THEY ARE IMPLEMENTED IN THE ABSTRACT CLASS AND ARE SUPPOSED TO BE INHERITED BY IT'S CHILD CLASSES AS THEY ARE...!!

      So,option A for Q#9 should also be correct!

      Delete
    3. Hi Rahul,
      Yes, you are right. I must have missed it. Thanks for pointing out. Option A is also correct for Q9. The answers corrected now.

      Anand

      Delete

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers