Core Java Questions Flashcards

1
Q

Explain each keyword in public static void main(String args[])?

A

a. This is the method that is called by the JVM when starting a program
b. Public - is an access modifier that says that this method is accessible by any class
c. static - says that this method can be accessed without instantiating a class
d. void - the method returns no value
e. String args[] - contains command line arguments that can be passed while running the program

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

What is an object and what is a class

A

a. Class - is the template or blueprint through which we can create objects in our programs
b. Object - is an instance of a class that represents an entity that the program is trying to model

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

What is static in Java

A

a. Keyword that is used for memory management
b. Can be used with classes, variables, methods and block
c. Static members belong to the class instead of a specific instance
d. Static means one per class

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

What is a constructor?

A

a. A special method that is used to instantiate an object
b. It has the same name as the class it is declared in
c. It has no explicit return type
d. It can be overloaded.

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

Types of constructors

A

a. Default Constructor - when it has no params and provides default values to the properties of the object
b. Parametized Constructor - used to provide different values to the properties of an object when instantiated

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

What is this keyword

A

a. can be used to refer to the current class instance variable
b. can be used to invoke current class method
c. this() can be used to invoke current class constructor
d. can be passed as an argument in the method call
e. can be passed as argument in the constructor
d. 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
7
Q

What is meant by the local variable and the instance variable

A

a. Local variable - is a variable that is declared inside a method
b. instance variable - is declared inside a class but outside a method

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

What is inheritance

A

a. Process by which a class gains the properties and methods of another class.

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

What is encapsulation

A

a. Process of hiding implementation details from other classes.

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

What is polymorphism

A

a. The ability to perform an action in several different ways
b. There are two type - Static(compile) time polymorphism and dynamic(runtime) polymorphism

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

What is method overloading

A

a. When a class has more than one method with the same name but different signature(different parameters)

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

What is method overriding

A

a. When a method of a superclass is inherited by a subclass and the implementation is changed.

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

What are the different access modifiers

A

a. Access modifiers - are keywords used to set the visibility of classes, constructors and data members
b. Default - visible only within the package
c. Private - visible within the class only
d. Protected - visible within the package and subclasses
e. Public - visible to all classes within or outside the package

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

What is an Interface

A

a. An interface is used to achieve abstraction and loose coupling
b. In Java, a class cannot extend more than one class but with interfaces, a class can implement as many interfaces as possible.
c. interfaces have methods that are abstract by default but have no implementation
d. They can also have variables which can be public, static and final by default.

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

What is an Abstract Class?

A

a. A class that is declared using the keyword abstract
b. It can have methods that don’t have a body (abstract) as well as concrete methods(those with a body)
c. An abstract class cannot be instantiated but it must be extended to use the abstract and non abstract methods

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

What is the difference between Abstract class and Interface?

A

a. Abstract class can extend only one class or abstract class while Interfaces can extend a number of interfaces
b. An abstract class can have both abstract and concrete methods while an interface can only have abstract methods
c. An abstract keyword is required for an abstract method in an abstract class but in an interface the abstract keyword is optional as the method by default is abstract
d. An abstract class can have protected and public abstract methods while an interface can have only public abstract methods
e. An abstract class can have static, final or static final variables with any access modifies but interfaces can only have public static final as the access modifier

17
Q

What is the final keyword ?

A

a. Final variables cannot be changed when initialized
b. Final method cannot be overridden
c. Final class cannot be subclassed

18
Q

What is a thread

A

a. The flow of execution of a program
b. They are used to perform tasks concurrently and asynchronously
c. Every java program has at least one thread called the main thread created by the JVM
d. The user can define their own thread by extending the Thread class or implementing the Runnable interface.

19
Q

What is the difference between String, StringBuffer and StringBuilder?

A

a. String is a sequence of characters, an immutable object
b. StringBuffer and StringBuilder are classes used to manipulate strings
c. StringBuilder is not thread safe but is faster when compared to StringBuffer
d. StringBuffer is thread safe and synchronized but is slower than StringBuilder.

20
Q

What is the difference between Array and ArrayList?

A

a. An array is a fixed length of items while ArrayList is dynamic in length
b. Arrays support primitive data types and objects while Arraylist can only store objects
c. Array performs faster than ArrayList due to being strongly typed

21
Q

What is the Collection Framework?

A

a. Is a combination of classes and interfaces that are used to store and manipulate data that is in the form of objects

22
Q

What is the difference between List and Set in Java

A

a. List is an interface in java used to create an ordered collection of objects that allows duplicates and null values.
b. List implementations are - ArrayList, LinkedList
c. Set is an interface used to create an unordered collection that doesn’t allow duplicate elements and can only have one null value.
d. Set implantations are - HashSet, TreeSet

23
Q

What is the difference between Set and Map

A

a. Set allows for an ordered collection of items while Map contains key value pairs
b. Set contains unique values where Map can contain unique keys with duplicate values.

24
Q

What is the difference between HashMap and HashTable

A

a. HashMap is non synchronized while HashTable is synchronized
b. HashTable is thread safe and can be shared between multiple threads
c. HashMap doesn’t guarantee the order of the map will remain constant over time.

25
Q

What is an Exception and the different types of Exceptions

A

a. Is an unwanted event that interrupts the normal flow of a program
b. There are two types - Checked and Unchecked
c. Checked - are checked by the compiler at the time of compilation eg SQLException, IOException, ClassNotFoundException
d. Unchecked exceptions are those not checked during compile time by the compiler
e. Eg ArithmeticException, NullPointerException. ArrayIndexOutOfBounds

26
Q

What are the exception handling keywords

A

a. try - used to
b. catch
c. finally
d. throw - used to throw an exception
e. throws - delcare an exception