Beginning Java5 QuizS2C Home « Beginning Java5 Quiz

The questions in this first quiz on Java5 are on the topics covered in the Java5 - Beginning Java5 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.

Beginning Java5 Lessons Description Question Range
Lesson 1 - Getting StartedIn this lesson we look at the code structure of a Java source file and look at some syntax.
Lesson 2 - Code Structure & SyntaxFor this lesson we look at the code structure of a Java source file and look at some syntax.1 - 8
Lesson 3 - Primitive VariablesThis lesson is about the primitive variable data types available in Java5.9 - 16
Lesson 4 - Method ScopeIn this lesson we look at method scope and what scope and its definition mean.17 - 24
Lesson 5 - OperatorsThis lesson is the first of two about the symbols used in Java for mathematical and logical manipulation.25 - 30
Lesson 6 - Bitwise OperatorsIn our second lesson on operators we look at the bitwise operators.31 - 36
Lesson 7 - Conditional StatementsIn this lesson we look at the conditional statements available in Java5.37 - 44
Lesson 8 - Loop StatementsThis lesson is all about the loop statements available in Java5.45 - 52

Java5 Quiz

The quiz below tests your knowledge of the material learnt in the Java5 - Beginning Java5 section of the site.

Question 1 : Which of the following goes inside a class definition?
- Methods go inside a class definition.
Question 2 : We use the java command to compile programs?
- We use the javac command to compile programs.
Question 3 : What does the void keyword specify?
- The void keyword specifies that nothing is returned from the method.
Question 4 : How do we run a file called Hello.class that contains a main() method?
- We run a file called Hello.class using java Hello.
Question 5 : Who has access to a public class?
- A public class can be accessed by anyone.
Question 6 : How do we compile a file called Hello.java?
- We compile a file called Hello.java using javac Hello.java
Question 7 : A statement contains methods?
- A method contains statements.
Question 8 : A source file should be named after its class?
- A source file should be named after it's class. In fact if the class is public and you name the source file differently and try to compile it you get an error.
Question 9 : Which is a valid declaration of a char type?
- A char type must be initialized as a numeric or a single character enclosed in single quotes.
Question 10 : An int can fit into a short?
- Integer types can fit into larger integer types but not smaller. So an int will not fit into a short.
Question 11 : Pick out the valid identifier?
- Identifiers can only start with a letter, the $ symbol or the _ symbol.
Question 12 : What are the two floating point primitive types?
- The two floating point primitive types are double and float.
Question 13 : Will the following code compile?

short s 65; byte b= 65; int i = s + b;
- Variable i is dynamically initialized at runtime from s and b so is fine.
Question 14 : What other integer types can a short fit into?
- A short can fit into an int and a long
Question 15 : What happens when we compile the following statement?

float f = 16.54;
- Doesn't compile as the compiler thinks we are passing a double to a float.
Question 16 : We can use numerics in any position of an identifier?
- You can't start an identifer with a numeric.
Question 17 : What are variables created within method scope known as?
- Variables created within method scope known are known as local variables.
Question 18 : Parameters passed to a method are within method scope?
- Parameters passed to a method are included as part of the method scope.
Question 19 : What is wrong with the following snippet of code?

public static void main (String[] args) { float aFloat = 12.34F; double aDouble;
double bDouble = aFloat * aDouble; }
- Variable aDouble is not initialized (variable bDouble is dynamically initialized at runtime).
Question 20 : What is the lifetime of a local variable?
- A local variable exists until the method it is declared in finishes executing.
Question 21 : local variables declared within an inner scope are available to the outer scope?
- Outer scopes know nothing about local variables declared within an inner scope.
Question 22 : How do we create a block of code?
- We create a block of code using curly braces
Question 23 : Is the following snippet of code valid?

public static void main (String[] args) { int a = 0;
while (a < 2) { int a = 0; a++; } }
- Variables declared in an inner scope cannot have the same name as variables declared in an outer scope.
Question 24 : Each time a scope is entered the local variables that are declared with an initializer are reset to the initializer value.?
- local variables that are declared with an initializer are reset to the initializer value each time the scope is entered .
Question 25 : What values do a and b hold after the following expressions?

int a = 5; int b = 10; b = a++;
- A char type must be initialized as a numeric or a single character enclosed in single quotes.
Question 26 : What symbol do we use for XOR (exclusive OR)?
- The ^ symbol is used for XOR (exclusive OR).
Question 27 : When could we use the || short-circuit operator?
- The || Short circuit operator can be more efficient when the first operand of an OR has a true value and therefore the OR criteria is met.
Question 28 : We can convert a boolean to an int using a cast?
- The boolean type is not only incompatible but also inconvertible with other types.
Question 29 : What is the result of a logical operator?
- The result of a logical operation is the boolean type.
Question 30 : Which code will work?
- C is the correct answer. We don't have to use the same type for the cast as long as the the cast is not widening. We will just get back the first 8 bits of the value.
Question 31 : What symbol do we use for the bitwise AND operation?
- We use the & symbol for the bitwise AND operation.
Question 32 : What is the bitwise OR useful for?
- The bitwise OR is useful for switching bits on.
Question 33 : We can use bitwise operators on any primitive type?
- The bitwise operators only work with the integer types (byte, short, int and long).
Question 34 : Which of the following is the symbol for the bitwise signed right shift?
- >>> is the symbol for the bitwise signed right shift.
Question 35 : What value do bits have that are said to be swiched on?
- Swiched on bits have the value 1.
Question 36 : What bitwise shift operator retains the sign?
- The bitwise signed operator (>>>) retains the sign. In fact all bits shifted into the target will be signed.
Question 37 : What will be output from this code snippet?

boolean a = false; if (a = true) {System.out.println("true");} else {System.out.println("false");}
- The snippet will output true. This is because if ( a = true) actually assigns true to 'a'.<br />if ( a == true) would check 'a' for equality and return false.
Question 38 : When would we branch to else in an simple if...else construct?
- When the condition expression returns 'false' we branch to 'else'.
Question 39 : What will be output from this code snippet?

long intValue = 1;
switch (intValue) {
case 1:
System.out.print(" 1 matched");
case 2:
System.out.print(" 2 matched");
break;
default:
System.out.println(" Default as no case matches");
}
- The snippet will not compile as <code>long</code> is not a valid type for a <code>switch</code> expression.
Question 40 : What will be output from this code snippet?

int intValue = 3;
// A switch where a case matches.
switch (intValue) {
default:
System.out.print(" Default as no case matches");
case 1:
System.out.print(" 1 matched");
case 2:
System.out.print(" 2 matched");
}
- The <code>default</code> statement can appear anywhere.<br />In this case it appears first and as no <code>break</code> the rest of the <code>switch</code> is processed
Question 41 : What will be output from this code snippet?

int a = 20, b = 20; if (a & b) {System.out.println("Result was true");}
- The result of an <code>if</code> conditional expression must be a boolean and therefore requires use of a relation operator or logical operands. <br />In this case <code>(a & b)</code> returns an <code>int</code> from a bitwise operation.
Question 42 : What will be output from this code snippet?

char charValue = 'C';
switch (charValue) {
case A:
System.out.print(" A matched");
case B:
System.out.print(" B matched");
break;
default:
System.out.print(" Default as no case matches");
}
- This will not compile as the <code>case</code> checks need to be integers or enclosed in single quotes to compare against the <code>char</code> type.
Question 43 : How many operands does the ? : operator take?
- The <code>? :</code> operator is known as a <code>tenary</code> operator as it takes three operands.
Question 44 : What will be be the value of maxInt after this code snippet is run?

int int1 = 1234, int2 = 5678, maxInt; maxInt = (int1 > int2) ? 111 : 222;
- <code>maxInt</code> will hold the value 222 as the result of <code>(int1 > int2)</code> is <code>false</code> and therefore 222 gets assigned.
Question 45 : Which loop statement was introduced in Java5 to iterate over collections?
- The <em>enhanced for</em> loop statement was introduced in Java5 to iterate over collections.
Question 46 : The condition component of a for loop is always tested againt the control variable for true after each iteration of the loop?
- False. It is tested BEFORE each iteration of the loop.
Question 47 : Which statement will immediately exit a loop?
- The <code>break</code> statement will immediately exit a loop.
Question 48 : What will be output from this code snippet?

long [] anArray = {22}; for (int i : anArray) { System.out.println("Array element = " + i); }
- Doesn't compile. You can't fit a <code>long</code> into an <code>int</code>.
Question 49 : Which loop statement is always guaranteed to execute at least once?
- The <em>do while</em> loop is always guaranteed to execute at least once.
Question 50 : What will be output from this code snippet?

boolean a = false; while (a = true) { System.out.println("In the loop""); a = false; }
- This code will compile fine, but when run creates an endless loop as each time we hit the <em>while</em> loop we assign <em>true</em> to the conditional expression.
Question 51 : How could we force the next iteration of a loop?
- The <code>continue</code> statement will force the next iteration of a loop.
Question 52 : The break statement exits all loops and continues at the next line?
- The <code>break</code> statement only exits the current loop.
To exit all loops you would need to use the <code>break</code> statement with a <code>label</code> statement that points to the outermost loop.
Quiz Progress Bar Please select an answer

What's Next?

The next quiz on Java5 is all about objects and classes.

go to home page Homepage go to top of page Top