Java Flashcards

1
Q

What is an If Statement?

A

An if statement is a boolean expression followed by one more statements. The block of code inside the if statement will run if the boolean expression is true.

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

What is a Switch statement?

A

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.

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

What are Loop statements in Java?

A

A loop statement allows us to execute a statement or group of statements multiple times.

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

What is a While loop?

A

A while loop statement in Java programming language repeatedly executes a target statement as long as a given condition is true.

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

What is a For loop?

A

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to be executed a specific number of times.

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

What is a do while loop?

A

A do…while loop is similar to a while loop, except that a do…while loop is guaranteed to execute at least one time.

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

What is an enhanced For loop?

A

This is mainly used to traverse collection of elements including arrays.

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

What is the usage of “This” keyword?

A

In java, this is a reference variable that refers to the current object.
Here is given the 6 usage of java this keyword.

    this can be used to refer current class instance variable.
    this can be used to invoke current class method (implicitly)
    this() can be used to invoke current class constructor.
    this can be passed as an argument in the method call.
    this can be passed as argument in the constructor call.
    this can be used to return the current class instance from the method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the use of “new” keyword in Java?

A

The new keyword is a Java operator that creates an object. The new operator instantiates a class by dynamically allocating(i.e, allocation at run time) memory for a new object and returning a reference to that memory.

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

What is the use of the “super” keyword in Java?

A

The super keyword in Java is a reference variable which is used to refer immediate parent class object.

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

What is the use of “static” keyword in Java?

A

The static keyword belongs to the class than an instance of the class. The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc.

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

What is the use of “final” keyword in Java?

A

In the Java programming language, the final keyword is used in several contexts to define an entity that can only be assigned once. Once a final variable has been assigned, it always contains the same value

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

What is a String?

A

A string is an object that represents a sequence of characters. For example text.

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

What are some string handling methods?

A
char charAt(int index)  - Returns the character at the specified index. 
    int compareTo(Object o)  - Compares this String to another Object. 
    String concat(String str)  - Concatenates the specified string to the end of this string. 
    boolean equals(Object anObject) - Compares this string to the specified object. 
    boolean equalsIgnoreCase(String anotherString) - Compares this String to another String, ignoring case considerations.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a package in Java?

A

A java package is a group of similar types of classes, interfaces and sub-packages.

Package in java can be categorized in two form, built-in package and user-defined package.

There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.

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

What are advantages of packages?

A

Java package is used to categorize the classes and interfaces so that they can be easily maintained.
Java package provides access protection.
Java package removes naming collision.

17
Q

What are the 3 ways to access package outside of package?

A
import package.*;
import package.classname;
fully qualified name.
If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages. 
If you import package.classname then only declared class of this package will be accessible.
18
Q

What is an Interface?

A
An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.
Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that a class implements.
    Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.
19
Q

What are differences between interface and class?

A

You cannot instantiate an interface.
An interface does not contain any constructors.
All of the methods in an interface are abstract.
An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
An interface is not extended by a class, it is implemented by a class.
An interface can extend multiple interfaces.

20
Q

What are exceptions?

A

Exceptions are events that occur during the execution of programs that disrupt the normal flow of instructions (e.g. divide by zero, array access out of bound, etc.).

In Java, an exception is an object that wraps an error event that occurred within a method and contains:

Information about the error including its type 
The state of the program when the error occurred 
Optionally, other custom information
21
Q

What are checked and unchecked exceptions?

A

Checked: are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using throws keyword. (array index out of range)
Unchecked are the exceptions that are not checked at compiled time.

22
Q

What is a try, catch and what purpose does finally serve?

A

A try statement is used to catch exceptions that might be thrown as your program executes. You should use a try statement whenever you use a statement that might throw an exception
A try block is always followed by a catch block, which handles the exception that occurs in the try block.
A finally block contains all the crucial statements that must be executed whether an exception occurs or not.

23
Q

What is an Array?

A

Java array is an object which contains elements of a similar data type. It is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array.

Array in java is index-based, the first element of the array is stored at the 0 index.

24
Q

What is a Queue?

A

The queue collection is used to hold the elements about to be processed and provides various operations like the insertion, removal etc. It is an ordered list of objects with its use limited to insert elements at the end of the list and deleting elements from the start of list i.e. it follows the FIFO or the First-In-First-Out principle.

LinkedList, ArrayBlockingQueue and PriorityQueue are the most frequently used implementations.

25
Q

What is a List Interface?

A

The List interface extends Collection and declares the behavior of a collection that stores a sequence of elements.

Elements can be inserted or accessed by their position in the list, using a zero-based index.
A list may contain duplicate elements.
In addition to the methods defined by Collection, List defines some of its own, which are summarized in the following table.
Several of the list methods will throw an UnsupportedOperationException if the collection cannot be modified, and a ClassCastException is generated when one object is incompatible with another.
26
Q

What is an ArrayList?

A

It provides us dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed.

    ArrayList inherits AbstractList class and implements List interface.
    ArrayList is initialized by a size, however the size can increase if collection grows or shrunk if objects are removed from the collection.
    Java ArrayList allows us to randomly access the list.
    ArrayList can not be used for primitive types, like int, char, etc. We need a wrapper class for such cases.
27
Q

What is the Set Interface?

A

A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction.
The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.
Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ.

28
Q

What is the Map Interface?

A

The Map interface maps unique keys to values. A key is an object that you use to retrieve a value at a later date.

Given a key and a value, you can store the value in a Map object. After the value is stored, you can retrieve it by using its key.
Several methods throw a NoSuchElementException when no items exist in the invoking map.
A ClassCastException is thrown when an object is incompatible with the elements in a map.
A NullPointerException is thrown if an attempt is made to use a null object and null is not allowed in the map.
An UnsupportedOperationException is thrown when an attempt is made to change an unmodifiable map.