Java Quiz
What is the output of the following code? String x = "bob"; String y = x; String b = new String("bob"); String a = "bob"; if(x == y) System.out.print("1"); if(x == b) System.out.print("2"); if(x == a) System.out.print("3");
13
How many objects are created with the following code? String a = new String(); String b = a; a = "Hello";
2
What is the use of the '%' operator?
It returns the remainder of a division operation between integers
Which interface does not extend the Collection interface?
Map
Should you catch all Throwable objects?
No, Errors are Throwable, and should not be caught
The "transient" keyword...
Prevents data from being serialized
What will be the result of this method, if we pass in a Bird object? public void whatIsThis(Animal a) { if (a instanceof Dog) System.out.println("This is a Dog!"); else System.out.println("I don't know what this is"); }
"I don't know what this is"
Marshalling is...
the process of converting an object instance into a data format that describes it
What is the output of the following code? for(byte i = 0; i < 5; i ++){ System.out.print(i); }
01234
What is the output of the following code? int i = 0; while(i < 5){ if(i == 0)System.out.print(0); i++; System.out.print((i > 2 ? 1 : 2)); }
022111
Given the command line argument in quotes "1 2 3" what will be the output of the following code? public static void main(String[] args) { for(String temp: args){ System.out.print(temp); } }
1 2 3
Abstract classes:
are made to be extended not instantiated.
Which one of the following is a valid class declaration?
class example implements Serializable, Runnable? (NOT static abstract class example implements Serializable extends ArrayList)
What is the output of the following code? char a = 'a'; a += 5; System.out.println(a);
f
Interfaces are a form of inheritance
False
The @Override annotation...
Tells the compiler that the method is intended to override a superclass method? (NOT Overrides any compiler warnings about bad code in the method.)
How many String objects have been created at the end of this code sequence? String a = "Hello"; String b = "World"; a = a + b; a = new String("Hello"); String c = "World";
Four?
Which of these is the symbol for an annotation?
@
"Collections" is?
A class filled with static methods used to manipulate collections
What is a constructor?
A special method called at instantiation, used to create an object and initialize variables
Which of these is a valid location for an annotation in Java 8?
Before a class declaration, before the use of a type, before a method declaration (all of these)
Which of these is a consequence of removing a method declaration from an interface after it has been implemented?
Code would break whenever an interface reference is used to polymorphicaly call the method in an instance of an implementing class.
Which of these is a consequence of adding a method declaration to an interface after it has been implemented?
Every implementing subclass would need to add a definition for the new method? (NOT There are no consequences, interfaces can always be modified to add methods)
A do-while statement will never execute if it's conditional check is false.
False
True or False: A class in Java can extend multiple parent classes
False
True or False: An array is a type of Collection in Java
False
True or False: Child classes must have the same variable values as their parent class
False
True or False: Data given no access modifier can only be accessed within the same class in which it is declared.
False
True or False: If you write a constructor, the compiler will still give you a default, no-argument constructor.
False
True or False: class members marked "private" can be accessed by classes outside of the package.
False
True or False: if the class Car extends class Automobile, then an instance of Automobile can access Car methods
False
What is the boolean value of this statement? String string1 = "TEST"; string1.equals(string1.toLowerCase());
False
You should catch errors where possible
False
public class Question { public static int main(String[] args) { } }
The main() method returns an int
A benefit of describing data with a language like XML is...
The recipient application does not need to be written in the same language as the application that sent the data. The sender and recipient can use schemas to validate the structure of sent or received data (Both?)
The equals() method is equivalent to the == operator, unless overridden
True
True or False. Because LinkedLists in java implement List, they have an index
True
True or False: A finally block will always execute, regardless of whether an exception is caught
True
True or False: Because sets have no ordering, there is no way to retrieve a specific object from a Set immediately.
True
True or False: Inherited behaviors can be overriden in a subclass
True
True or False: Java can be run on any machine that has a JVM
True
True or False: Mapping annotations are exclusive - an annotated method will only be accessible to requests sent to a matching URL, with no partial matches
True
True or False: Mapping with annotations requires you to identify the mapped classes in your configuration document.
True
True or False: The process of sending data in Java is called "streaming"
True
True or False: The relationship between a class and its members is a, HAS-A" relationship
True
True or False: You can string multiple if...else statements together
True
True or False: clone() creates a new instance of an object with all the properties initialized to the same values
True
True or False: new String() will create a new String object in memory, even if there is a matching String in memory already
True
True or False: the following code statement is valid: while(true) {}
True
True or false: the following code is valid? public class Question { public static void myMethod(Object o) {} public static void main(String[] args) { myMethod(new String("Test")); } }
True
You can have multiple catch blocks for a single try block.
True
Can you catch an Error?
Yes, but you should not - an application can rarely handle them properly, or continue running after they occur
