JAVA Flashcards
A checked exception must be handled by the developer.
Select one:
a. True
b. False
True
1) Checked: are the exceptions that are checked at compile time.
2) Unchecked are the exceptions that are not checked at compile time.
A class can extend multiple interfaces.
Select one:
a. True
b. False
False
A class can implement more than one interface at a time.
A constructor Select one: a. Can be overloaded b. Has a return type c. Has to exist in the source for a class.
a. Can be overloaded
Constructor name must be same as its class name Constructor must have no explicit return type
A final class Select one: a. Must contain an abstract method b. Has immutable attributes c. Cannot be extended d. Cannot extend a class
c. Cannot be extended
A final class can be instantiated but not extended (subclassed).
A List extends Collection to handle sets, which must contain unique elements
Select one:
a. True
b. False
b. False
A Set cannot contain duplicate elements while a List can.
A static method can
Select one:
a. Cannot be overloaded
b. Can only be called if at least one instance of its class exists in the program
c. Be called even if no instances of its class exist in the program
d. Only be called from a static method
c. Be called even if no instances of its class exist in the program
A static method belongs to the class rather than object of a class. A static method can be invoked without the need for creating an instance of a class. Static method can access static data member and can change the value of it.
A sub class can access private members of the super class
Select one:
a. False
b. True
a. False
Private members (both fields and methods) are only accessible inside the class they are declared or inside inner classes.
A subclass can access its parent class’s private attributes.
Select one:
a. True
b. False
b. False
If a data member within a class is declared as private, then it is only accessible from within that same class, nested classes in the same file and access via reflection.
A try block must have a catch block
Select one:
a. True
b. False
b. False
If any of the code in the try block can throw a checked exception, it has to appear in the throws clause of the method signature. If an unchecked exception is thrown, it’s bubbled out of the method.
An anonymous class inherits directly from Object.
Select one:
a. False
b. True
a. False
An anonymous inner class is an inner class that is declared without using a class name at all – and that of course is why it’s called an anonymous class.
An EOFException catch block can be coded after an IOexception catch block.
Select one:
a. True
b. False
b. False
EOFException extends IOException
EOF = End of File
IO = Input Output
An interface can extend another interfaces.
Select one:
a. True
b. False
a. True
Arrays are static whereas ArrayLists are dynamic.
Select one:
a. False
b. True
b. True
Array: Simple fixed sized arrays that we create in Java
ArrayList : Dynamic sized arrays in Java that implement List interface
Bytecode is written in binary.
Select one:
a. False
b. True
a. False
Bytecode is the compiled format for Java programs. Once a Java program has been converted to bytecode, it can be transferred across a network and executed by Java Virtual Machine (JVM)
Code inside of a finally block will always be run if its try block has been entered.
Select one:
a. False
b. True
a. False
If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.
FileReader reads bytes.
Select one:
a. False
b. True
a. False
FileReader is used for reading streams of characters.
Final methods can be overridden.
Select one:
a. False
b. True
a. False
You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses.
Garbage collection can be forced by Select one: a. Using the gc() method b. Extending the GarbageCollector class. c. None of the above d. It can never be forced.
d. It can never be forced.
JVM manages memory for you.
HashMaps can be used to allow null values and a null key.
Select one:
a. True
b. False
a. True
Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.)
How does a Collection differ from a Map?
Select one:
a. A Collection has an iterator whereas a Map does not.
b. Collection inherits from iterable, whereas Map does not.
c. Collections have a type whereas Maps have key-value pairs.
d. All of the Above.
d. All of the Above.
Map
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
Collection
A collection represents a group of objects, known as its elements
If a try/catch block has multiple catches, the more general exception must come after the more specialized ones.
Select one:
a. True
b. False
a. True
a. True Interfaces have Select one: a. None of the above b. Implicit final methods c. Implicitly abstract methods d. Implemented methods
c. Implicitly abstract methods
A Java interface is a bit like a class, except a Java interface can only contain method signatures and fields. An Java interface cannot contain an implementation of the methods, only the signature (name, parameters and exceptions) of the method.
Java supports multi-level inheritance but does not support multiple inheritance.
Select one:
a. False
b. True
b. True
Diamond Problem
Method over-riding is
Select one:
a. Method with different number or type of parameters
b. A way to prevent certain classes from calling a method
c. Same method signature but different implementation
d. Changing an instance method to a static method
c. Same method signature but different implementation
Compile Time
Object variables are stored in the \_\_\_\_\_\_\_\_\_\_\_, and local variables are stored in the \_\_\_\_\_\_\_\_\_\_\_\_\_. Select one: a. Heap; Stack b. Trace; Heap c. Disk; RAM d. Stack; Heap
a. Heap; Stack
The heap is sometimes divided into two areas (or generations) called the nursery (or young space) and the old space. The nursery is a part of the heap reserved for allocation of new objects. When the nursery becomes full, garbage is collected by running a special young collection, where all objects that have lived long enough in the nursery are promoted (moved) to the old space, thus freeing up the nursery for more object allocation. When the old space becomes full garbage is collected there, a process called an old collection.
Protected variables cannot have their values changed once set.
Select one:
a. True
b. False
b. False
When you mark a variable as final, it means that you cannot assign a different object reference to that variable
String and StringBuffer are mutable.
Select one:
a. False
b. True
a. False
String is immutable, if you try to alter their values, another object gets created, whereas StringBuffer and StringBuilder are mutable so they can change their values.
StringBuilder is Select one: a. Thread-safe b. Immutable c. Mutable d. Slower than StringBuffer
c. Mutable
StringBuffer is thread-safe as it runs on a single thread.
The == operator
Select one:
a. Checks for equality of two objects based on the attributes
b. None of the above.
c. Checks for equality of two primitive variables
d. Checks for data type equality only
c. Checks for equality of two primitive variables
The changes to the instance variables of one object also have an effect on the instance variables of the other object
Select one:
a. True
b. False
b. False
Instance variable is the variable declared inside a class
The compareTo method is in this interface. Select one: a. Compared b. Serializable c. Comparator d. Comparable
d. Comparable
The first line of a constructor is always Select one: a. super() or this() b. Calling the garbage collector c. Initialization of the main method d. Extending the Main class
a. super() or this()
The parent class’ constructor needs to be called before the subclass’ constructor. This will ensure that if you call any methods on the parent class in your constructor, the parent class has already been set up correctly.
The following is NOT true about Java Select one: a. Memory is not allocated automatically b. Object oriented programming c. Write once run anywhere
a. Memory is not allocated automatically
Java objects reside in an area called the heap. The heap is created when the JVM starts up and may increase or decrease in size while the application runs.
The following is true about Exceptions Select one: a. Can be handled in a try block b. They can be compile time or run-time c. They are identical to errors d. None of the above
b. They can be compile time or run-time
Checked exceptions − A checked exception is an exception that occurs at the compile time, these are also called as compile time exceptions. These exceptions cannot simply be ignored at the time of compilation, the programmer should take care of (handle) these exceptions.
Unchecked exceptions − An unchecked exception is an exception that occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.
The naming convention for method names is called Select one: a. Camel Casing b. Class Casing c. Pascal Casing d. Upper Casing
a. Camel Casing
Java naming convention is a rule to follow as you decide what to name your identifiers such as class, package, variable, constant, method
The postfix increment operator returns the value of the operand before being incremented.
Select one:
a. True
b. False
a. True
The String class is Select one: a. None of the above b. final and immutable c. final and static d. final and abstract e. final and mutable
b. final and immutable
The synchronized keyword can be used with a class.
Select one:
a. False
b. True
a. False
The Java programming language provides two basic synchronization idioms: synchronized methods and synchronized statements.
The transient keyword is used to enable serialization of variables.
Select one:
a. False
b. True
a. False
The transient keyword in Java is used to indicate that a field should not be serialized.
To execute the program you need which of the following?
Select one:
a. A public static void main method that takes command line arguments
b. A main method that returns a status code.
c. The classes must be compiled to .object files.
d. A Main class
a. A public static void main method that takes command line arguments
Variables associated with a particular object are called: Select one: a. Stack variables b. Local variables c. Instance variables d. Heap variables
c. Instance variables
Instance variables are declared in a class, but outside a method, constructor or any block. When a space is allocated for an object in the heap, a slot for each instance variable value is created.
What collection will NOT allow duplicate elements? Select one: a. Map b. Tree c. Set d. List
c. Set
The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.
What interface must a class implement to allow it to be added to a TreeSet? Select one: a. Comparable b. Comparator c. Orderable d. TreeIndexable
a. Comparable
This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.
What is a final variable? Select one: a. Cannot be extended b. Cannot be changed c. Cannot be overloaded d. Cannot be overridden
b. Cannot be changed
In Java, when final keyword is used with a variable of primitive data types (int, float, .. etc), value of the variable cannot be changed.
What is the use of the synchronized keyword?
Select one:
a. Prevents a variable from being serialized
b. Allows access to a resource one thread at a time
c. Allows a variable to be serialized
d. Synchronizes values from one POJO into another
b. Allows access to a resource one thread at a time
Intrinsic locks play a role in both aspects of synchronization: enforcing exclusive access to an object’s state and establishing happens-before relationships that are essential to visibility.
What type of value does hashCode() return? Select one: a. byte b. Object c. long d. int
d. int
When overriding compareTo method, returning a positive result means that this object should come before the other object.
Select one:
a. True
b. False
b. False
CompareTo method must return negative number if current object is less than other object, positive number if current object is greater than other object and zero if both objects are equal to each other.
Which method adds or replaces a value for a key in a Map? Select one: a. update b. add c. put d. insert
c. put
put(K key, V value)
Associates the specified value with the specified key in this map (optional operation).
Which of the following allows duplicates ? Select one: a. ArrayList b. TreeSet c. HashSet d. ResultSet
a. ArrayList
Set does not allow duplicates
Which of the following exceptions cannot be declared in the throws clause of a method overriding the method: public void go() throws IOException Select one: a. IOException b. NullPointerException c. Exception
c. Exception
Which of the following is a marker interface? Select one: a. Comparable b. Runnable c. Serializable d. Executable
c. Serializable
Which of the following is an example of a checked exception? Select one: a. ArrayIndexOutOfBoundsException b. NullPointerException c. FileNotFoundException
c. FileNotFoundException
Checked Exceptions
Exception
Unchecked Exception
Errors
RuntimeException
Which of the following is not a Wrapper Class Select one: a. Boolean b. Long c. Short d. None of the above e. Double
d. None of the above
All the wrapper classes (Integer, Long, Byte, Double, Float, Short) are subclasses of the abstract class Number.
Which of the statements regarding the join() method of Thread is correct?
Select one:
a. The thread object on which join() has to be called must be waiting for a lock.
b. The thread object on which join() has to be called must be sleeping.
c. None of these
d. The thread calling the join() method must hold the lock of the other thread object.
c. None of these
join()
Waits for this thread to die.
You can force the garbage collection to work by using the gc method
Select one:
a. False
b. True
a. False
System.gc() simply is a hint to the garbage collector that you want it to do a collection. There is no way to force and immediate collection though as the garbage collector is non-deterministic.
You can use the add method to insert elements into a HashTable.
Select one:
a. True
b. False
b. False
put(K key, V value)
Maps the specified key to the specified value in this hashtable.