This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Section 1 Mock test questions

Tom Tolman
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have made up these Section 1 Mock test questions. Please provide feedback and point out any problems with them.

1. Which two of the following lines will not compile?

A) int [] array = null;
B) char letters[] = new [] char {'a','b','c','d'};
C) boolean [] b [], c [], d;
D) double scores[][] = new double[5][];
E) Object [] objs = new Object [] { new Object(), new Object()};
F) int i; short shorts[] = new short[i = 5];
G) long [] big = new long [3] {(char) 'c', (int) 6.7, 0xfee };

2. What will the following code output?

A) Will not compile
B) Runtime exception thrown
C) 5433
D) 2344
E) 345
F) 2343
G) 234

3. What is the minimum number of lines of the following code that would need to be modified for this to compile?

1) 0 lines
2) 1 line
3) 2 lines
4) 3 lines
5) 4 lines
6) 5 lines

4) Which two of the following statements are true?

1) You can never declare a class to be static
2) You need an instance of an outer class to access an inner class
3) The keyword strictfp can not be applied to an inner class
4) An anonymous inner class can utilize polymorphism
5) A method local inner class can be instantiated in any method in the class
6) If you overload a method in an anonymous inner class you can invoke the method

5) Which of the following classes will have a default constructor created for it?

1) Just class Bolt
2) Just class Screw
3) Just class Bolt and class Nut
4) Just class Bolt and class Glue
5) Just class Bolt, class Nut, and class Glue
6) All of the classes

6) What will the compiler insert at the line //Here

1) super();
2) base();
3) super(this);
4) this.super(d);
5) default();

7 Which two of the following lines could be compiled if inserted at the line //Here

1 String j; return j;
2 return String;
3 return null;
4 return new Object();
5 return "Hot";
6 return 'd' + 'r' + 'y';

8 Which one statement is true?

1 Arrays can not be used as a return type
2 You can never return null from a method
3 You can return null if the return type is void
4 An overridden method can change the return type
5 It is not valid to ever use the statement return; by itself on a line
6 None of the above statements are true

1. Objective 1.1

Answers: B, G are incorrect syntax for an array.
A is correct to assign null to the Object reference array.
B should read char letters[] = new char [] {'a','b','c','d'};
C array [] can float
D an array of arrays can be set to a new array of arrays
E you can dynamically create create elements for an array
F it is valid to dynamically create an array with the result of an expression
G the [3] is incorrect, replacing it with [] makes a valid line

2. Objective 1.1

B - Runtime exception thrown. When the code tries to access a[a.length] it will be outside the bounds of the array

3. Objective 1.2

2 - The first problem is that the nonabstract class PeanutButter needs to override the abstract method long Taste(). This could be fixed either by changing the abstract Butter method to be abstract public int Taste() or by changing the return type in the method of PeanutButter.

The second problem is that the method Taste() in PeanutButter accessess the private member variable of the base class Butter. Private is only within the class itself.

4. Objective 1.2


1- false, you can declare an inner class (or a so called nested class) to be static
2- true, this would be false for a static inner class- but a static inner class is not considered an inner class, it is a nested class
3- false An inner class is a member of the outer class, and you can apply the keyword strictfp to it
4 -true An anoymous inner class can utilize polymorphism
5 - false A method local inner class can only be invoked in the method it is defined
6 - false There is no way to invoke new methods defined for an inner class

5 - Objective 1.3

3 Just class Bolt and class Nut

Class Bolt has no constructor- it will have a default constructor created.
Class Screw has a constructor defined- it does not need a default constructor
Class Nut has a method with a return type and no constructor. A default is created.
Class Glue has a constructor, it does not need a default constructor

6 Objective 1.3

Answer 1 super();

7 Objective 1.4

3, 5 are both valid
1 J is utilized before being initialized
2 this could be new String(); to work
3 null is a valid value for String
4 even though String derives from Object, it is not a valid return type
5 "Hot" is turned into a string
6 'd' + 'r' + 'y' is a character addition, not a string

8 Objective 1.4

6 - None of the above statements are true

1 Arrays are valid return types
2 You can return null if the return type is derived from Object
3 A void return type can not return anything, not even null
4 An overridden method can NOT change the return type
5 return; by itself can be used in a method with a void return type

Review of objectives for Section 1:

Section 1: Declarations and Access Control

1 Write code that declares, constructs and initializes arrays of any base type using any of the permitted forms both for declaration and for initialization.

2 Declare classes, nested classes, methods, instance variables, static variables and automatic (method local) variables making appropriate use of all permitted modifiers (such as public, final, static, abstract, etc.). State the significance of each of these modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers.

3 For a given class, determine if a default constructor will be created and if so state the prototype of that constructor.

4 Identify legal return types for any method given the declarations of all related methods in this or parent classes.
vydhehi paruchuri
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have a question about 7 question.

I compiled the code and I know that when the return type is String we can't return an reference of Object class.

Since Object is super class for String, why can't we return a reference to Object class?

Tom Tolman
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about it this way- the compiler wants to guarantee that any class returned can be used in an appropriate way by the calling code. This means that any methods which could be called on the object in the calling code needs to be supported.

Object contains a number of methods (such as equals())
String contains even more methods (such as length())

If you returned an Object, any polymorphic calls to equals would compile and run at least (although be incorrect) because they are in the base class, however any polymorphic calls to length() would be invalid. The length() would be accessing instance variables which did not exist. Hence the compiler prevents you from returning classes from HIGHER on the hierarchy tree. It will allow you to return classes LOWER on the hierarchy tree, because they will have all the variables and methods required to handle any call on the base class.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic