Core Java Flashcards

1
Q

What are the Memory Allocations available in Java?

A

Class Memory
Heap Memory
Stack Memory
Program Counter-Memory
Native Method Stack Memory

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

What is the default value stored in Local Variables?

A

Neither the Local Variables nor any primitives and Object references have any default value stored in them.

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

Explain the expected output of the following code segment?
public class Simplilearn

{

public static void main (String args[])   

{  

    System.out.println(100 + 100 +“Simplilearn");   

    System.out.println(“E-Learning Company" + 100 + 100);  

}  

}

The answers for the two print statements are as follows.

A

-200Simplilearn
- E-Learning Company100100

Explanation:
Javas behavoiur is to interprete your given values.

Your cases: 1)

System.out.println(100 + 100 +”Simplilearn”);
100 is an integer, so Java interprets the “+” as arihmetic operator. 100 + 100 equals 200.

The next “+” cant be of arithmetic type, because of “Simplilearn” wich is an String.

2)

System.out.println(“E-Learning Company” + 100 + 100);
If you give Java an String at first (same is if you youse this as variable value), Java interprets the whole line as String. This is because “+” can´t be an arithmetic type, when the previous is a string.

So for Java your Output equals “E-Learning Company100100”.

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

Define Copy Constructor in Java

A

A Copy Constructor in Java is a constructor that initializes an object through another object of the same class.

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

Define Wrapper Classes in Java.

A

In Java, when you declare primitive datatypes, then Wrapper classes are responsible for converting them into objects(Reference types).

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

What is a singleton class in Java? And How to implement a singleton class?

A

A class that can possess only one object at a time is called a singleton class. To implement a singleton class given steps are to be followed:

Make sure that the class has only one object
Give global access to that object

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

Define package in Java.

A

The package is a collective bundle of classes and interfaces and the necessary libraries and JAR files. The use of packages helps in code reusability.

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

What is an Exception?

A

An Exception handling in Java is considered an unexpected event that can disrupt the program’s normal flow. These events can be fixed through the process of Exception Handling.

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

What is the final keyword in Java?

A

The final is a keyword word in Java that is used while declaring values to variables making value constant throughout the program’s execution.

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

What happens when the main() isn’t declared as static?

A

the program may be compiled correctly but is so ambiguous that it throws the run time error “NoSuchMethodError.”

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

Why is the main method static in Java?

A

Java’s main() function is static by default, allowing the compiler to call it either before or after creating a class object.

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

What is the best way to inject dependency? Also, state the reason.

A

Constructor injection. A class requesting its dependencies through its function Object() { [native code] } is the most typical instance of dependency injection. Since the client cannot be constructed without the required dependencies, this guarantees that it is always in a correct state.

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

Find Factorial of a number using for loop

A

int num = 10;
long factorial = 1;
for(int i = 1; i <= num; ++i)
{
// factorial = factorial * i;
factorial *= i;
}
System.out.printf(“Factorial of %d = %d”, num, factorial);
}

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

Define System.out.println()

A

System.out.println() in Java outputs the argument that was supplied to it.

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

Can you explain the Java thread lifecycle?

A

A thread can be in any of the following states in Java. These are the states:

New: A new thread is always in the new state when it is first formed. The function hasn’t been run yet, thus it hasn’t started to execute for a thread in the new state.

Active: A thread switches from the new state to the active state when it calls the start() method. The runnable state and the running state are both contained within the active state.

Blocked or Waiting: A thread is either in the blocked state or the waiting state when it is inactive for a while (but not indefinitely).

Timed waiting: When we use the sleep () method on a particular thread, we are actually engaging in timed waiting. The thread enters the timed wait state using the sleep () function. The thread awakens when the allotted time has passed and resumes execution where it left off.

Termination: A thread that has been terminated means it is no longer active in the system. In other words, the thread is inactive and cannot be revived (made active again after being killed).

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

How is the ‘new’ operator different from the ‘newInstance()’ operator in java?

A

Both the new operator and the newInstance() method are used to create objects in Java. If we already know the kind of object to create, we can use the new operator; however, if the type of object to create is supplied to us at runtime, we must use the newInstance() function.