Java Flashcards

1
Q

What is Java Development Kit & what does it include?

A

In order to start writing the code in Java, we need to install Java Dev. Kit
JDK includes:
- Compiler - will compile for Java a source code into java-byte code, which isn’t unique machine code & will be executed by Java Virtual Machine (JVM)
- Java Runtime Environment (JRE) contains JVM & core java libraries
- Tools for Java development (archiver, docs generator, etc.)

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

What is a method?

A

A method is a set of codes which is referred to by name & runs when it’s called. Each method has its own name. We need method to reuse the code: define the code once & use it many times

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

What is a main method?

A

Main method is a special method that actually runs the program. Everything in the main method will be executed when we run the program from top to bottom

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

What is a constructor?

A

It’s a special method to create an object

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

What’s a default constructor?

A

If we don’t define any constructors by ourselves java will provide one default (empty) constructor.

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

What is the difference between the constructor & the method?

A
  • Constructor doesn’t have a return type & its name must be the same as a class name
  • Java provides the default constructor if we don’t define it
  • Constructor isn’t inherited by child class
  • Method has a return & its name may or may not be the same as a class name
  • Method isn’t provided buy Java
  • Methods are inherited by child class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between local & instance variables?

A
  • Local variables are declared in methods, constructors, or blocks.
  • Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block.
  • Access modifiers cannot be used for local variables.
  • Local variables are visible only within the declared method, constructor, or block.
  • Instance variables are declared in a class, but outside a method, constructor or any block.
  • Instance variables hold values that must be referenced by more than one method, constructor or block
  • Instance variables can be declared in class level before or after use.
  • Access modifiers can be given for instance variables.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the OOP concepts in Java you know?

A

Object Oriented Programming. In Java, we have 4 concepts:

Encapsulation, Inheritance, Abstraction, Polymorphism.

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

What’s an encapsulation in java and how do we achieve it?

A

It’s data protection(hiding) mechanism from the client code. We make instance variables as private and we provide public getters and setters. use pojo class

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

What’s inheritance in java?
Can you give an example of using the inheritance in your project?

A

It is a process where one class can inherit visible properties and methods from another class. Parent-child relationship (super class - subclass relationship). Inheritance is used for not creating the object. Using extends keyword

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

What’s abstraction in java?

A

When we focus on what object does instead of how it does it by hidding implementation details. We achvieve abstraction by using abstract method.

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

What’s polymorphism in java?

A

Polymorphism is the ability of the object to take many different forms.

List list = new ArrayList<>();
list = newLinkedList<>();

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

What’s POJO?

A

Plain Old Java Object. Usually, encapsulated objects referred as POJO.

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

What’s super class?

A

It’s parent class in inheritance.

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

What’s the class which every class will inherit from? Why?

A

java.lang.Object. To give generic behaviors for every object. For example, equals, hashCode, toString.

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

How many classes we can extend at once?

A

Just one because java supports only single type inheritance.
*** Java doesn’t support multiple inheritance.

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

Why Java doesn’t support multiple inheritance?

A

Java doesn’t support multiple inheritance in classes because of “Diamond Problem”.
Due to this Java does not support multiple inheritance i.e., you cannot extend more than one other class

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

What’s the difference between method overriding and method overloading?

A
  • Overloading is when we have multiple methods with the same name but different arguments
  • Overriding is when we override the parent class or interface method in the child class.
  1. Most of the method signatures should be the same
  2. Child class access modifier should be the same or more visible
  3. Return type should be the same or covariant
  4. Child class cannot declare bigger exception than parent class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What does ‘this’ and ‘super’ keyword mean in java?

A
  • ‘super’ is used to access the parent class properties and methods.
  • ’ this’ is used to access current object properties and methods.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Access modifiers

A
  • public - the member accessible from anywhere in the project
  • protected - accessible within the same package & inside subclasses
  • default - accessible within the same package only
  • private - accessible within the same class only
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Method Arguments

A

Arguments are used to provide the data to the method.
Methods can have multiple arguments with different types

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

What is variable?

A

Variable is a container that can hold piece of information. There are different type of variables for different data types in Java.

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

What kind of logical operators do you know? And how do they work?

A

Logical operators are used to check whether an expression is true or false. They are used in decision making.
&& (Logical AND) expression1 && expression2
true only if both expression1 and expression2 are true

|| (Logical OR) expression1 || expression2
true if either expression1 or expression2 is true

! (Logical NOT) !expression
true if expression is false and vice versa

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

What’s a static keyword in Java?

A
  • Static variables and methods belong to the class, not to a specific object.
  • We don’t need to create an object to call them, we use them by class name.

public FileHelper {
public static String path = “ABC”;
public static String readContent() {

return

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

What is final keyword in Java?

A
  • final variable cannot be changed (reassinged)
  • final class cannot be inherited
  • final method cannot be overridden
26
Q

How to prevent your method to be ever overridden?

A
  • make it final
27
Q

What is static block in Java?

A

Block of code that will be executed one time when this class is used
static {
. . .
. . .
}

28
Q

What is initializer block?

A

Block of code that run before constructor

29
Q

What is the relationship between parent class and child class?

A

Child class inherits visible variables and methods from the parent class

30
Q

Can one class extend multiple classes at the same time in Java?

A

No, we cannot

31
Q

Can one class implement multiple interfaces at the same time?

A

Yes, we can

32
Q

What is difference between abstract class and interface?

A
  • Interface can have only abstract methods (except static and default) and abstract class can have abstract and regular methods and properties
  • We can extend one abstract class and implement multiple interfaces
  • Abstract class can have abstract and non-abstract methods.
  • Interface can have only abstract methods. Since Java 8, it can have default and static methods also.
  • Abstract class doesn’t support multiple inheritance. - Interface supports multiple inheritance.

-Abstract class can have final, non-final, static and non-static variables.
- Interface has only static and final variables.

  • Abstract class can provide the implementation of interface. - Interface can’t provide the implementation of abstract class.
  • The abstract keyword is used to declare abstract class. - The interface keyword is used to declare interface.
  • An abstract class can extend another Java class and implement multiple Java interfaces.
  • An interface can extend another Java interface only.
  • An abstract class can be extended using keyword “extends”.
  • An interface can be implemented using keyword “implements”.
  • A Java abstract class can have class members like private, protected, etc.
  • Members of a Java interface are public by default.

Example: Example:
public abstract class Shape{ public interface rawable{
public abstract void draw(); void draw();
} }

An Interface in Java programming language is defined as an abstract type used to specify the behavior of a class. An interface in Java is a blueprint of a class. A Java interface contains static constants and abstract methods. The interface in Java is a mechanism to achieve abstraction

https://www.javatpoint.com/difference-between-abstract-class-and-interface

33
Q

Can you give me an example of using abstraction in your project?

A
  • I don’t use abstraction
    or
  • I use abstract templates to locate elements in my framework and we implement them in my steps class
34
Q

Can you give me an example of using polymorphism in your project?

A

WebDriver driver = new ChromeDriver();

WebDriver driver = new FirefoxDriver();

35
Q

What is the helper class for array and collection (for sorting, and searching)?

A

Arrays is a helper class for arrays
Arrays.sort();

36
Q

How to get number of elements in the array?

A

.length -> without parentheses
int[] nums = {1, 2, 3, 4, 5, 6};
System.out.println(nums.length);

37
Q

What is collection framework in Java?

A

It is framework provided by Java to work with collection of data
List, Set, Queue, Map (it is not part of the framework)

38
Q

How does ArrayList work internally?

A

ArrayList works internally with array

39
Q

What is the initial capacity (size of internal array) of ArrayList?

A

10

40
Q

What is Set in Java?

A

It is unique collection of data, doesn’t allow duplicates
The List is an ordered collection of objects and the List can contain duplicate values
SetTable synchronized doesn’t maintain insertion order

41
Q

What is Map in Java?

A

Key value data collection and keys are unique in map put()

42
Q

How do you iterate over the map?

A

We can iterate over the map by using keySet() method that returns all keys. Once we have keys we can easily find out values

43
Q

What is the difference between HashMap and HashTable?

A

hashTable doesn’t allow null key and null value, synchronized, doesn’t maintain insertion order, thread-safe
HashMap allows one null key and multiple null values,

44
Q

What is TreeMap? What is LinkedHashMap?

A

TreeMap - sorted by keys implementation of the map
LinkedHashMap - maintains insertion order

45
Q

What is Queue data structure?

A

FIFO - First In, First Out data structure

46
Q

What is the helper class for collection framework in Java (for sorting, searching)?

A

Collections is a helper class for collection framework

47
Q

What is Stack data structure?

A

LIFO - Last In, First Out data structure

48
Q

Could you explain about upcasting vs downcasting?

A

Person <- Student

// downcasting
Person obj = new Student();
Student student = (Student)obj;
Student.study();

// upcasting
Student obj2 = new Student();
Person person = obj2;

49
Q

How to handle exceptions in Java?

A

By using try catch statement

try {


} catch (TypeOfException e) {
// handle it
}

50
Q

When do you get NullPointerException?

A

When object is null and we try to use some of the property or method to this project

String str = null;
System.out.println(str); // NullPointerException

if (str == null) {
}

51
Q

What is the difference between runtime and checked exceptions?

A
  • we must handle checked exceptions but it is optional to handle runtime exceptions
  • all checked exceptions extend from Exception class (except RuntimeException)
52
Q

What does finally block do?

A
  • it always runs (if there is exception or not) after try catch
  • usually it is used to close resources
53
Q

Can you catch multiple exceptions in the same try statement?

A

Yes, we can
try {
} catch (NullPointerException e) {
} catch (RuntimeException e1) {
}

54
Q

What is Error? Do you handle or throw errors in your project?

A

Error is system level failure. Errors are thrown by Java Virtual Machine and they should not be handles in application level

55
Q

final vs finally vs finalize()

A
  • Final:
  • final is a keyword, it is used to apply restrictions on class method and variable
  • if a class is marked as final then this class can not be inherited by any other class
  • finally:
  • finally is a block that is used for exception handling along with try and catch blocks
  • Finalize:
  • finalize() method is protected method of java.lang.object class, it is inherited to every class you create in java & it is used to perform some clean-up operations on an object before it is removed from memory
56
Q

What is a StringPool?

A

It is a memory for String objects in the heap. Java will reuse the same String values from StringPool to save memory.

57
Q

String vs StringBuilder vs StringBuffer

A

STRING STRINGBUFFER STRINGBUILDER
Storage constant HEAP Heap
Area String pool
Modifiable no(immutable) YES(mutable) YES(mutable)
Thread Safe YES YES NO
Performance FAST VERY SLOW FAST

Immutable means values can not be changed once it’s created:

58
Q

Most common Java Exceptions

A

NullPointerException.
ArrayIndexOutOfBoundsException.
IllegalStateException.
ClassCastException.
ArithmeticException.
IllegalArgumentException.

59
Q

What are the wrapper classes in Java?

A

Object representations of the primitives in Java
byte -> Byte
short -> Short
int -> Integer
long -> Long
float -> Float
double -> Double
char -> Character
boolean -> Boolean

String str = “123”;
int num = Integer.parseInt(str);

60
Q

When do you use while loop? And when do you use for loop?

A

We use while loop when we don’t know number of iterations in advance
We use for loop when we know number of iterations in advance

61
Q

What is the difference between Array and ArrayList?

A
  • Array is fixed size and ArrayList is resizable
  • Array can work with primitives and Objects but ArrayList works only with Objects
62
Q

What is Varags in Java?

A
  • By using Varags we can create a method that can take dynamic number of arguments
  • it works inside the method exactly as Array
  • varag can be used in method argument only and only one varag per method

public int sum(int . . . nums) {
int sum = 0;
for (int num : nums) {
sum += num;
}
return sum;
}