JAVA Flashcards

1
Q

A checked exception must be handled by the developer.
Select one:
a. True
b. False

A

True

1) Checked: are the exceptions that are checked at compile time.
2) Unchecked are the exceptions that are not checked at compile time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

A class can extend multiple interfaces.
Select one:
a. True
b. False

A

False

A class can implement more than one interface at a time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
A constructor
Select one:
a. Can be overloaded
b. Has a return type
c. Has to exist in the source for a class.
A

a. Can be overloaded

Constructor name must be same as its class name
Constructor must have no explicit return type
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
A final class
Select one:
a. Must contain an abstract method
b. Has immutable attributes
c. Cannot be extended
d. Cannot extend a class
A

c. Cannot be extended

A final class can be instantiated but not extended (subclassed).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

A List extends Collection to handle sets, which must contain unique elements
Select one:
a. True
b. False

A

b. False

A Set cannot contain duplicate elements while a List can.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

A sub class can access private members of the super class
Select one:
a. False
b. True

A

a. False

Private members (both fields and methods) are only accessible inside the class they are declared or inside inner classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

A subclass can access its parent class’s private attributes.
Select one:
a. True
b. False

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

A try block must have a catch block
Select one:
a. True
b. False

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

An anonymous class inherits directly from Object.
Select one:
a. False
b. True

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

An EOFException catch block can be coded after an IOexception catch block.
Select one:
a. True
b. False

A

b. False

EOFException extends IOException
EOF = End of File
IO = Input Output

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

An interface can extend another interfaces.
Select one:
a. True
b. False

A

a. True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Arrays are static whereas ArrayLists are dynamic.
Select one:
a. False
b. True

A

b. True

Array: Simple fixed sized arrays that we create in Java
ArrayList : Dynamic sized arrays in Java that implement List interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Bytecode is written in binary.
Select one:
a. False
b. True

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Code inside of a finally block will always be run if its try block has been entered.
Select one:
a. False
b. True

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

FileReader reads bytes.
Select one:
a. False
b. True

A

a. False

FileReader is used for reading streams of characters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Final methods can be overridden.
Select one:
a. False
b. True

A

a. False

You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
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.
A

d. It can never be forced.

JVM manages memory for you.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

HashMaps can be used to allow null values and a null key.
Select one:
a. True
b. False

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

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.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

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

a. True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q
a. True Interfaces have
Select one:
a. None of the above 
b. Implicit final methods
c. Implicitly abstract methods
d. Implemented methods
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Java supports multi-level inheritance but does not support multiple inheritance.
Select one:
a. False
b. True

A

b. True

Diamond Problem

24
Q

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

A

c. Same method signature but different implementation

Compile Time

25
Q
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

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.

26
Q

Protected variables cannot have their values changed once set.
Select one:
a. True
b. False

A

b. False

When you mark a variable as final, it means that you cannot assign a different object reference to that variable

27
Q

String and StringBuffer are mutable.
Select one:
a. False
b. True

A

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.

28
Q
StringBuilder is
Select one:
a. Thread-safe
b. Immutable
c. Mutable 
d. Slower than StringBuffer
A

c. Mutable

StringBuffer is thread-safe as it runs on a single thread.

29
Q

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

A

c. Checks for equality of two primitive variables

30
Q

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

A

b. False

Instance variable is the variable declared inside a class

31
Q
The compareTo method is in this interface.
Select one:
a. Compared
b. Serializable
c. Comparator
d. Comparable
A

d. Comparable

32
Q
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

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.

33
Q
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

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.

34
Q
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
A

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.

35
Q
The naming convention for method names is called
Select one:
a. Camel Casing 
b. Class Casing
c. Pascal Casing
d. Upper Casing
A

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

36
Q

The postfix increment operator returns the value of the operand before being incremented.
Select one:
a. True
b. False

A

a. True

37
Q
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
A

b. final and immutable

38
Q

The synchronized keyword can be used with a class.
Select one:
a. False
b. True

A

a. False

The Java programming language provides two basic synchronization idioms: synchronized methods and synchronized statements.

39
Q

The transient keyword is used to enable serialization of variables.
Select one:
a. False
b. True

A

a. False

The transient keyword in Java is used to indicate that a field should not be serialized.

40
Q

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. A public static void main method that takes command line arguments

41
Q
Variables associated with a particular object are called:
Select one:
a. Stack variables
b. Local variables
c. Instance variables
d. Heap variables
A

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.

42
Q
What collection will NOT allow duplicate elements?
Select one:
a. Map
b. Tree
c. Set 
d. List
A

c. Set

The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.

43
Q
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

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.

44
Q
What is a final variable?
Select one:
a. Cannot be extended
b. Cannot be changed 
c. Cannot be overloaded
d. Cannot be overridden
A

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.

45
Q

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

A

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.

46
Q
What type of value does hashCode() return?
Select one:
a. byte
b. Object 
c. long
d. int
A

d. int

47
Q

When overriding compareTo method, returning a positive result means that this object should come before the other object.
Select one:
a. True
b. False

A

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.

48
Q
Which method adds or replaces a value for a key in a Map?
Select one:
a. update
b. add
c. put 
d. insert
A

c. put

put(K key, V value)
Associates the specified value with the specified key in this map (optional operation).

49
Q
Which of the following allows duplicates ?
Select one:
a. ArrayList 
b. TreeSet
c. HashSet
d. ResultSet
A

a. ArrayList

Set does not allow duplicates

50
Q
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
A

c. Exception

51
Q
Which of the following is a marker interface?
Select one:
a. Comparable
b. Runnable
c. Serializable 
d. Executable
A

c. Serializable

52
Q
Which of the following is an example of a checked exception?
Select one:
a. ArrayIndexOutOfBoundsException
b. NullPointerException
c. FileNotFoundException
A

c. FileNotFoundException

Checked Exceptions
Exception

Unchecked Exception
Errors
RuntimeException

53
Q
Which of the following is not a Wrapper Class
Select one:
a. Boolean
b. Long
c. Short
d. None of the above 
e. Double
A

d. None of the above

All the wrapper classes (Integer, Long, Byte, Double, Float, Short) are subclasses of the abstract class Number.

54
Q

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.

A

c. None of these

join()
Waits for this thread to die.

55
Q

You can force the garbage collection to work by using the gc method
Select one:
a. False
b. True

A

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.

56
Q

You can use the add method to insert elements into a HashTable.
Select one:
a. True
b. False

A

b. False

put(K key, V value)
Maps the specified key to the specified value in this hashtable.