Objects & Classes QuizS2C Home « Objects & Classes Quiz

The questions in this second quiz on Java6 are on the topics covered in the Java6 - Objects & Classes section of the site. The table below lists the lessons, a description of the lesson content and the the quiz question number range.

Lesson Summary

Click on a lesson in the table to go to that lesson for a refresher on the topics for that lesson.

Objects & Classes Lessons Description Question Range
Lesson 1 - ArraysIn this lesson we look at arrays and how to use them in Java6.1 - 8
Lesson 2 - Class Structure & SyntaxFor this lesson we looked at class structure and syntax within our Java programs.9 - 16
Lesson 3 - Reference VariablesThis lesson is about reference variables and how to use them within our Java programs.17 - 23
Lesson 4 - MethodsWe have used methods throughout the lessons so far so its time for a thorough investigation of what we can do with these members.24 - 33
Lesson 5 - Instance Variables & ScopeThis lesson is about instance variables, how to use them and their scope.34 - 39
Lesson 6 - ConstructorsIn this lesson we learn about constructors, which we can use to instantiate objects of our classes.40 - 46
Lesson 7 - Static MembersFor this lesson we investigate what static means and how to apply it to our class members.47 - 52
Lesson 8 - EnumerationsIn our final lesson of this section we go through enumerations which were introduced in Java6.53 - 60

Java6 Quiz

The quiz below tests your knowledge of the material learnt in the Java6 - Objects & Classes section of the site.

Question 1 : What is the second step of array creation?
- Array allocation is the second step of creating an array.
Question 2 : We can look at an element of an array after array declaration?
- We can only look at an element within an array after allocation. After declaration no size has been allocated.
Question 3 : What will be output from this code snippet?

String strArray[] = {"one", "aa", "c", "rt", "je"};
System.out.println(strArray[1]);
- The output will be <code>aa</code>. Arrays are zero-index based
Question 4 : What will be output from this code snippet?

String strArray3[] = new String[3]{"one", "rt", "je"};
System.out.println(strArray3[1]);
- The code doesn't compile. When using a single statement to create an array, no size should be specified.
Question 5 : Which of the following statements will compile?

int intArray1[][]; // A
int[][] intArray2; // B
int[] intArray3[]; // C
- All the statements will compile as the square brackets can follow the type, the array name or be split between the two for multi dimensional arrays.
Question 6 : What will be output from this code snippet?

String[] strArray1;; // Array declaration
strArray1 = new String[1]; // Array allocation
System.out.println(strArray1[1]);
- The output will be <code>null</code> as space has been allocated so array is initialized to default.
Question 7 : We can have a maximum of two dimensions for arrays?
- There is actually no limit to the amount of dimensions an array can have although in practical terms arrays are rarely more than three dimensions.
Question 8 : What will be result when this code snippet is run?

int intArray1[]; // 1
intArray1 = int[2]; // 2
intArray1[0] = 22; // 3
intArray1[1] = 44; // 4
System.out.println(intArray1[0]); // 5
- The <code>new</code> keyword is mandatory for multiple statement array creation.<br />The code would compile and output <code>22</code> if line 2 changed to <code>intArray1 = new int[2];</code>.
Question 9 : What are instance variables and class methods commonly known as?
- Instance variables and class methods are commonly known as <em>members</em>.
Question 10 : What describes an object's state?
- Instance variables describe an object's state.
Question 11 : A Class must have at least one instance variable?
- There is no requirement for a class to have any instance variables
Question 12 : What describes an object's behaviour?
- Class methods describe an object's behaviour.
Question 13 : How do we instantiate an object?
- We instantiate an object using the <code>new</code> keyword.
Question 14 : How do we access class members?
- We access class members using dot notation.
Question 15 : A class is an object?
- A class is a blueprint for object construction.
Question 16 : Instance variables apply to the whole class?
- Instance variables apply to each instance of a class, not to the whole class.
Question 17 : Where are objects stored?
- Objects are stored on the heap.
Question 18 : Where are reference variables stored?
- Reference variables are stored on the stack.
Question 19 : How does the JVM reclaim memory?
- The JVM reclaims memory through garbage collection.
Question 20 : Two ways an object becomes eligible for garbage collection are when it goes out of scope and no live references exist and when all pointers to it are set to null. What is the third?
- An object becomes eligible for garbage collection when the object is unreachable.
Question 21 : How can we check the reference type of an object?
- We can check the reference type using the <code>instanceof</code> operator.
Question 22 : What is output from the following code snippet?

public class Testinstanceof {
public static void main (String[] args) {
String moggy = new String();
Cat string = new Cat();
if (string instanceof String) {; // Check for Cat instance
System.out.println("string is a String.");
}
}
}
- You will get a compiler error for <em>inconvertible types</em>.
Question 23 : A reference variable holds an objects state?
- false - A reference variable holds a pointer to an object on the heap.
Question 24 : Method declarations can have up to how many components?
- Method declarations can have up to 6 components
Question 25 : The required elements of a method are the the method name, a pair of parenthesis () a body between braces {} and?
- Methods must have a <em>return type</em>.
Question 26 : We can have more than one method with the same method name in a class?
- We can have more than one method with the same name in a class and this is known as overloading.
Question 27 : What return type is used when we don't want to return anything from a method?
- The <code>void</code> <em>return type</em> is used when we don't want to return anything from a method.
Question 28 : Is the following code snippet valid?
static void addNumbers(int ... number, String string) {
System.out.println("Total = " + string);
}
- When using <em>varargs</em> they must come last in the parameter list.
Question 29 : What do we have to be careful of when overloading methods that use varargs
- We have to be careful of ambiguity when overloading methods that use varargs. The compiler needs to resolve parameter lists to one method.
Question 30 : Is the following method declaration valid?
static void addNumbers(int ... number, String ... string) {
System.out.println("Total = ");
}
- When using <em>varargs</em> only one is allowed in the parameter list.
Question 31 : How do we pass a value back from a method
- We use the <code>return</code> keyword.
Question 32 : What are the method name and method parameters components collectively known as?
- Collectively these components are known as the method signature.
Question 33 : When we pass an argument to a method what are we are actually passing?
- When we pass an argument to a method we are actually passing a copy of the argument and this is known as <em>pass-by-value</em>
Question 34 : What are variables created within a class and outside any method known as?
- Variables created within a class and outside any methods are known as instance variables.
Question 35 : Instance variables live on the stack?
- Instance variables live on the heap.
Question 36 : Instance variables are unique to their class
- Instance variables are unique to an instance of the class not the class itself.
Question 37 : What is the lifetime of an instance variable?
- An instance variable exists as long as the instance of the class it belongs to.
Question 38 : What do instance variables describe?
- Instance variables describe an objects state.
Question 39 : Instance variables are declared within a method?
- Instance variables are declared within a class and outside any methods
Question 40 : How many parameters in a default constructor?
- The default constructor has no parameters.
Question 41 : What differentiates a constructor and a method?
- constructors have no return type.
Question 42 : The compiler always provides a default no-arg constructor?
- The compiler only provides a default no-arg constructor when no other constructors are present.
Question 43 : When you have more than one constructor in a class these are known as?
- When you have more than one constructor in a class these are known as overloaded constructors.
Question 44 : What does the keyword this refer to?
- The keyword <code>this</code> refers to the current object.
Question 45 : When using overloaded constructors what must we ensure?
- When using overloaded constructors we must ensure that they have different argument lists, so the compiler can differentiate between them.
Question 46 : We invoke constructors within the same class using the new keyword?
- We invoke constructors within the same class using the <code>this</code> keyword
Question 47 : What do static variables apply to?
- Static variables apply to a class
Question 48 : We use static and which keyword to define a Java constant?
- We use the <em>final</em> keyword.
Question 49 : We prefix static variables with the name of the method?
- We prefix static variables with the name of the class.
Question 50 : Which runs first after a class has loaded?
- Static initializer blocks.
Question 51 : Can we initiailize Java constants in a method?
- Java constants can only be initialized when they are defined or in a static initializer block.
Question 52 : When defining a Java constant which comes first?
- It doesn't matter in which order these keywords appear.
Question 53 : What are enumerations?
- Enumerations are lists of named constants
Question 54 : How do we declare an enumerated type?
- We use the <code>enum</code> keyword.
Question 55 : What are the identifiers TOMATO, CHICKEN and PRAWN in the code below known as?

enum Soup { TOMATO, CHICKEN, PRAWN }
- The identifiers TOMATO, CHICKEN and PRAWN are <em>enumeration constants</em>.
Question 56 : How do we create an enum singleton?
- We create an enum singleton using a single <em>enumeration constant</em> in our enum type.
Question 57 : Does the following switch statement use the enum below correctly?

enum Soup { TOMATO, CHICKEN, PRAWN }
Soup soup = Soup.PRAWN;
switch (soup) {
case Soup.CHICKEN:
System.out.println("Our " + soup + " soup has meat in it!");
case Soup.PRAWN:
System.out.println("Our " + soup + " soup has fish in it!");
}
- The <code>case</code> statements are incorrectly qualified with the enum type <code>Soup</code> and so the <code>switch</code> statement uses the enum incorrectly.
Question 58 : What does the predefined enum method values() return?
- the predefined enum method <code>values()</code> returns an array consisting of a list of enumeration constants.
Question 59 : How many objects are instantiated from the following enum declaration?

enum Soup { TOMATO, CHICKEN, PRAWN }
- An object is automatically instantiated for each <em>enumeration constant</em>, so in this case 3 objects.
Question 60 : What are enumeration constants implicitly declared as?
- All <em>enumeration constants</em> are implicitly declared as <code>public</code> and <code>static</code> members of their enum type.
Quiz Progress Bar Please select an answer

What's Next?

The next quiz on Java6 is all about OO concepts.

go to home page Homepage go to top of page Top