Most Frequent Java Interview Questions Flashcards

1
Q

Explain JDK?

A

JDK stands for Java Development Kit.

JDK is the tool necessary to compile, document, and package Java programs.

It contains JRE + development tools

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

Explain JRE?

A

JRE stands for Java Runtime Environment

JRE refers to a runtime environment in which Java bytecode can be executed. It contains libraries and resources that a Java program needs to run.

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

Explain JVM

A

JVM stands for Java Virtual Machine

It is a virtual machine that enables a computer to run Java program compiled to Java bytecode.

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

Explain public static void main(String[] args)

A
  • public: Is an access modifier, which is used to specify who can access this method. Public means that this method will be accessible by any class.
  • static: It is a keyword in java which identifies it is class-based. main() is made static in Java so that it can be accessed without creating the instance of a class.
  • void: It is the return type of the main() method. Void means the method will not return any value.
  • main: It is the name of the method which is searched by the JVM as a startingn point for an application. It is the method where the main execution occurs.

Main it is the name of the method which is searched by the JVM as a starting point for an application. It is the method where the main execution occurs.

  • String[ ] args: It is the parameter passed to the main method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is Java not 100% object-oriented?

A

Java is not 100% object-oriented because it makes use of eight primitive data types which are not objects.

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

What are Java’s 8 primitive data types and their sizes?

A

boolean, byte, short, char, int, float, long, and double.

boolean 1 bit
byte 8 bit

short 16 bit
char 6 bit

int 32 bit
float 32 bit

long 64 bit
double 6 bit

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

Why Java is platform independent?

A

Java is platform-independent because it’s compiled to Java bytecode and can run any operating system.

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

What are wrapper classes in Java?

A

Wrapper classes convert Java primitives into objects. Every primitive data type has a class dedicated to it.

Since Java 5 the auto box feature was introduced.
Autoboxing is when the primitive type is automatically converted to an Object. An example is converting an int into an Integer object.

Unboxing is the opposite of autoboxing, where the object is converted back to a primitive type. An example, converting a Long object into a long primitive type.

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

What are constructors in Java?

A

In Java, constructors refer to a block of code that is used to initialize an object.

It must have the same name as the class, and it has no return type. It’s also automatically called when an object is created.

There are 2 types of constructors:

  • Default constructor: In Java, the default constructor does not take any inputs.
  • Parameterized Constructor: This constructor is able to initialize the object with provided values.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a singleton class in Java and how can we make a class singleton?

A

Singleton class is a class whose only one instance can be created at any given time, in one JVM.

A class can be made singleton by making its constructor private.

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

What is the difference between equals() and == (equal operator) in Java?

A

The equals operator and the equals() method are used in Java to compare two objects.

The == (equal operator) compares the memory location of two objects.

The equals() method compares the VALUE of both objects.

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

What are access modifiers in Java?

A

In Java, access modifiers are special keywords that are used to restrict access to classes, constructors, data members, and methods by other classes.

Java supports four types of access modifiers:

default
public
protected
private

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

Define a Java class, and define an object

A

A Java class is a blueprint that contains variables and methods to describe the behavior of an object.

An Object is an instance of a class that has state and behavior. The instance variables provides the state of the object, and

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

What is Kafka?

A

Kafka is a distributed publish-subscribe stream-processing messaging system.

It’s open-source software and its goal is to provide a unified, high-throughput, platform for handling real-time data feed.

Highly throughput
Scalability
Replication 
Durability 
Stream process
data loss
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Describe the “Highly throughput” feature in Kafka

A

Kafka is high throughput because supports millions of messages with modest hardware

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

Describe the scalability feature in Kafka

A

Kafka offers highly scalable distributed systems with no downtime

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

List the various components of Kafka

A

The four major components of Kafka are:

  • Topic
  • Producer
  • Brokers
  • Consumers
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

List the various components of Kafka

A

The four major components of Kafka are:

  • Topic
  • Producer
  • Brokers
  • Consumers
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Describe what is a topic in Kafka

A

In Kafka a topic is a stream of messages belonging to the same type.

  • Kafka can have as many topics as you want
  • Topics are identified by their name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Describe what is a topic in Kafka

A

In Kafka, a topic is a stream of messages belonging to the same type.

  • Kafka can have as many topics as you want
  • Topics are identified by their name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Describe what is a topic in Kafka

A

In Kafka, a topic is a stream of messages belonging to the same type.

  • Kafka can have as many topics as you want
  • Topics are identified by their name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Describe the role of the offset

A

The messages contained in the partitions are assigned a unique ID number and that is called the offset.
The role of the offset is to uniquely identify every message within the partition.

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

Kafka

Describe the role of the offset

A

The messages contained in the partitions are assigned a unique ID number and that is called the offset.
The role of the offset is to uniquely identify every message within the partition.

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

Kafka
T or F
Is the order of offsets guaranteed only within a partition?

A

True
Each partition is individual, and cannot write messages between partitions.

Example:
If offset 8 in partition 0 has been written the next offset will be 7 regardless of the offsets of others partitions.

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

Kafka
T or F
Can data get shared between partitions?

A

False

Partitions are individuals. Data cannot get shared between partitions.

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

In Kafka data is kept only for a limited time. What is the default time?

A

The default time is one week

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

Kafka

Once the data is written to a partition can it be changed?

A

No. Once the data in the partition is written it can’t be changed, updated, or swap. The data is immutable.

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

Kafka

Is the data assigned randomly to a partition?

A

Yes

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

Kafka

How can you ensure the data is being assigned to a specific partition?

A

By providing the partition key

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

Kafka

Define what is a broker?

A

A broker is a set of servers where the published messages are stores.

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

Kafka

What is the ideal number of brokers that topics should have?

A

Ideally 3.

If a broker is down, the other 2 can serve the data.

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

Explain the concert of Leader and Follower

A

In Kafka, every partition has one server which plays as the leader, and none or more servers that act as followers.

The leader performs the task of all read and write requests of that partition, while the role of the partition follower is to passively replicate the leader, in other words, it keeps reading data from the partition leader without lagging behind.

In the event of the Leader failing, one of the partition Followers will take on the role of the Leader. This ensures load balancing of the server and can still serve the data.

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

Kafka

What is a Replication factor?

A

A replication factor defines the number of copies of a topic in a Kafka cluster.

The replication value should be more than 1. Usually between 2 or 3, but 3 is the best option.
If one broker is down the other brokers can still serve the data.

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

What is encapsulation?

A

Encapsulation is the process of restricting direct access to an object’s variables and methods by making them ‘private’.

Encapsulation increases the security of data and making public getters and setters can make the class read-only or write-only.

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

Why Encapsulation?

A

Because:

  • Increase the security of data
  • Offer read-only when using getters, and write-only when using setters.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

What is inheritance?

A

Inheritance enables classes to inherit variables and methods from one class to another.

The class that inherits from another class is called a child class.
The class being inherited from is called parent class. 

To inherit variables and methods from a class, the keyword “extented” must be used in the child class.
This feature helps in reusing already defined code.

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

To inherit from a class, use the _____ keyword.

A

extend word

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

Inheritance, if a parent variable is private, can it be accessed by the child class?

A

No. If the variable in the parent class has an access modifier as private, the child classes will not be able to access it.

39
Q

Why And When To Use “Inheritance”?

A

Inheritance is useful for code reusability: reuse attributes and methods of an existing class when you create a new class.

40
Q

If you don’t want other classes to inherit variables and/or methods from a class, use the ____ keyword

A

Final keyword

41
Q

What is polymorphism?

A
Polymorphisim in Java allows an object to take many forms. 
It uses the methods inherited from another class to perform different action and return different values.
42
Q

Remember from the Inheritance chapter that we use the ____ keyword to inherit from a class.

A

extends

43
Q

Why And When To Use “Inheritance” and “Polymorphism”?

A

It is useful for code reusability. Reuse attributes and methods of an existing class when you create a new class.

44
Q

What is data “Abstraction”?

A

Abstraction is the process of hiding the implementation details and showing only functionality to the user.

An example would be an Interface, where it specifies what an object can do and not the how.
It shows the functionalit, the method’s sigantures and hides the implementation of ‘how’.

Abstraction is one of the core benifits of Java, because it allows the implementation to change or to add new implementation without affecting the existing code.

45
Q

What are inner classes?

A

Inner classes in Java refer to having a class within a class they belong together. Inner classes are used to make the code easier to read to maintainable.

46
Q

Collections

What are the Interfaces of the Collections Framework?

A

The interfaces of the Collections framework are:

  • List
  • Set

Map is not an interface of the Collections framework but is also a very popular data structure.

47
Q

Collections

What are the differences between List, Set, and Map?

A

The difference between List, Set, and Map is:

  • List stores the elements in order and allows to find the elements by index. Also allows duplicate elements and any amount of null values.
  • Set does not store the elements in order, does not allow duplicate elements, and allows only one null element.
  • Map works with key, value pairs do not allow duplicate keys, and allows null as the key and value, but some implementation does not allow null keys and values.
48
Q

Collections

What are the classes of the List interface?

A

The classes of the List interface are:

  • ArrayList
  • LinkedList
  • Vector
49
Q

Collections

What are the classes of the Set interface?

A

The classes of the Set interface are:

  • HashSet
  • LinkedHashSet
  • TreeSet
50
Q

Collections

What are the classes of the Map?

A

The classes of the Map are:

  • HashMap
  • HashTable
  • TreeMap
51
Q

When to use List, Set, and Map?

A

A List, can be used when we need to keep the insertion order.
- Imagine a restaurant, we need to keep the order of the incoming order.

Set, can be used when we need to store elements without duplications.
- An example, storing social securities, where the priority is to ensure there are not duplicate social security numbers.

  • Map, can be used when data is key-value pairs and need fast retrieval.
52
Q

Java

What are the differences between Public, Private, Default, and Protected?

A

Default - Accessible within the same package

Public - Is accessible from anywhere

Protected - Accessible within the package and subclasses

Private - Only members of the class have access

53
Q

What is a final variable?

A

When the value of the variable can’t be updated or changed.

54
Q

What is a final method?

A

When the method can’t be overridden in the child class.

55
Q

What is a final class?

A

When a class can’t be sub-classed.

56
Q

What is casting?

A

There are two types of casting:

Casting between numeric types means assigning the value of a primitive type to another type.

Casting primitive numbers and objects references.
Casting between numeric types, a larger value can be changed to a smaller value, for example, double to int.

Casting between objects references is to refer an object to a compatible class, or interface.

Example:
Parent class: Animal
Child class: Cat

The cat object can be cast into an animal object.

57
Q

What is the difference between Abstraction and Encapsulation?

A

The difference between Abstraction and Encapsulation, is that:

  • Abstraction hides the implementation information and only shows the function, in other words, allows focusing on ‘what’ the object does instead of the ‘how’.
  • Encapsulation hides all the code to avoid access.
58
Q

What are the differences between Overloading and Overriding?

A

Overloading means that multiple methods can have the same name with different parameters.

Overwriting is when the method is inherited in the child class from the parent class.

59
Q

Name few Java exceptions?

A

OutOfMemory, NullPointException, IndexOutOfBound

60
Q

What is “super” used for?

A

Super is used to call the parent’s methods and constructors from the child class.

61
Q

What is “this” keyword used for?

A

The “this” keyword can be used inside constructors and methods, and works as a reference to the current object.

62
Q

What is a finalize() method?

A

A finalize() method is called before an object is deleted or destroyed.

63
Q

What is the difference between an Array and ArrayList?

A

The difference between an Array and ArrayList is that the size of an array can’t be modified, if the user needs to add a new element it must create a new one and move all the data. In an ArrayList, the user can remove or add any element without creating a new one.

64
Q

What keyword do you use to add elements to an ArrayList?

A

To add an element to an ArrayList use the method add()

65
Q

What keyword do you use to find elements on an ArrayList?

A

To search for an element in an ArrayList use the method get()

66
Q

In inheritance, what is the difference between the private and final keyword?

A

In Inheritance, the keyword private refers to restricting access to the class, variable, or method from other classes.
The final keyword does not allow overriding methods in the parent class.

67
Q

When should you use an ArrayList vs a LinkedList?

A

ArrayList - ArrayLists are better when accessing elements because it takes constant time.

LinkedList - LinkedLists are better for manipulating data, and removing or adding elements at the beginning, middle, or end of the collection.

68
Q

Explain the main characters of a HashMap

A
  • Uses key, value pairs
  • HashMap is non-synchronized, meaning is not thread-safe
  • Can only store objects as keys and values
  • The HashMap allows to store only one null key and any amount of null values
69
Q

Explain the main characteristics of ArrayList

A
  • ArrayList is an implementation class of the List interface
  • The ArrayList is a resizable array
  • The elements store in the ArrayList can only be objects

Best to use when:
- ArrayLists are better for accessing data

70
Q

Explain the main characteristics of LinkedList

A
  • LinkedList in the implementation class of the List interface

Best to use when:

  • LinkedLists are better for manipulating data
  • Adding or removing elements from the beginning, middle, or end of the list
71
Q

Explain the main characteristic of a HashTable

A
  • Uses key, value pairs
  • The key will be hashed, and the hash code is used as the index at which the value is stored.
  • the HashTable is synchronized, meaning is thread-safe
  • The HashTable does not allow any null keys or values because they are not objects
72
Q

What is the difference between Vector and ArrayList?

A
  • They both extended class from the List interface

- Vector is a synchronized collection, and the ArrayList is not synchronized

73
Q

What is Association?

A

Hint: Compose of 2 instances

Association means that an instance of one class has a reference to an instance of another class.

Association is a way of describing reference between two or more classes using instance variables and an instance should be created before it is used.

There are two types of association:

  • Composition
  • Aggregation
74
Q

What are the benefits of Association?

A

The benefits of composition are:

  • Reuse code
  • Offers better test-ability of a class
75
Q

What are the features of Java 8?

A

The main features of Java 8 are:

  • Lambda Expressions
  • Functions Interface
  • Steam API
76
Q

From Java 8, what is a Stream?

A

A Stream is a sequence of elements

77
Q

What is the difference between Collection and Stream?

A

The main difference is that a Stream takes input from the Collection, and can be used along with lambdas to provide an output. A stream does not store elements, and doesn’t change the original data structure.

The collection stores data in data structures such as List, Set, or Map interfaces. Operations such as deleting, searching, and sorting can be performed in the data structure.

78
Q

What does the map() function does, and why use it?

A
The map() function can convert one object type into another object type.
An example, it can convert a String into an Integer
79
Q

What does the filter() method do? when you use it?

A

The filter() method uses the Predicate function. It takes an input and returns a boolean, either true or false.

An example the developer passes a list of integers, and uses the filter() method to check if each element is even or not.

80
Q

What does the flatmap() function do? why do you need it?

A

The flatmap() function is an extension of the map() method, just like the map() function, it converts an object type to another object type, however, the flatmap() can also combine two objects into one.

An example is if a list has a list, the developer can use the flatmap() to combine both lists into one list, meaning, one object.

81
Q

What is the difference between a map() and a flatmap() method?

A

Both flatmap() and map() methods can convert an object into another type object, however, the flatmap() method can combine two objects into one object.

82
Q

What’s the difference between intermediate and terminal operations on Streams?

A

The difference is that intermediate will return the stream of elements for the next method to use. An example of intermediate operations are the filter(), map() and peek() methods that will pass the stream of elements to the next method.

The terminal operations will not return the stream of elements, an example is the forEach() method.

83
Q

What does the peek() method do? when to use it?

A

The peek method() uses the consumer interface. It takes input and prints important information.

It’s usually used for debugging.

84
Q

How does the Function interface work?

A

The Function takes input and provides an out.
or
The Functions interface takes an argument and produces a result.

One of the common use cases of this interface is the Stream.map() method which takes an input, and convertes it to another object type.

85
Q

How does the Consumer interface work?

A

The consumer interface takes an argument, does not returns a result.

It is mostly used for debugging, and is the most common stream.the method used with the consumer interface is the peek() method.

86
Q

How does the Predicate interface work?

A

The predicate interface takes an object as an argument and returns a boolean as a result.

It is used in several Stream methods like the filter(), which uses Predicate function to filter unwanted elements.

An example is if a developer has a list of integers including odd and even numbers. The developer can use the filter() method to check each element, if it’s an even number or not.

87
Q

How does the Supplier interface work?

A

The supplier interface does not take an input, however, it produces an object as an output, and it has only one method, wich is the ‘get’()

88
Q

What is a functional interface in Java 8?

A

A functional interface is an interface that represents a function.
Also, if a method takes a functional interface can also accept a lambda expression.

89
Q

What is the difference between a normal interface and a functional interface?

A

The difference between a normal interface and a functional interface is that a normal interface can contain any amount of methods, and the functional interface can only contain one abstract method.

90
Q

Can you convert an array to Stream?

A

Yes. You can convert an array to a stream using the stream.of

91
Q

What is the purpose of static methods and variables?

A

The static methods and variables belong to the class, and not an object.

The static variables and methods can be called without creating an instance of the class.

92
Q

What is a static method?

A

A static method belongs to the class, and an instance of the class is not needed in order to call the static method.

93
Q

Can a static method access a non-static method?

A

No. Static methods can only access another static method.

94
Q

Can a non-static method access a static method?

A

Yes. Non-static methods can access non-static and static methods.