Core Java Flashcards
What are the Memory Allocations available in Java?
Class Memory
Heap Memory
Stack Memory
Program Counter-Memory
Native Method Stack Memory
What is the default value stored in Local Variables?
Neither the Local Variables nor any primitives and Object references have any default value stored in them.
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.
-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”.
Define Copy Constructor in Java
A Copy Constructor in Java is a constructor that initializes an object through another object of the same class.
Define Wrapper Classes in Java.
In Java, when you declare primitive datatypes, then Wrapper classes are responsible for converting them into objects(Reference types).
What is a singleton class in Java? And How to implement a singleton class?
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
Define package in Java.
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.
What is an Exception?
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.
What is the final keyword in Java?
The final is a keyword word in Java that is used while declaring values to variables making value constant throughout the program’s execution.
What happens when the main() isn’t declared as static?
the program may be compiled correctly but is so ambiguous that it throws the run time error “NoSuchMethodError.”
Why is the main method static in Java?
Java’s main() function is static by default, allowing the compiler to call it either before or after creating a class object.
What is the best way to inject dependency? Also, state the reason.
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.
Find Factorial of a number using for loop
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);
}
Define System.out.println()
System.out.println() in Java outputs the argument that was supplied to it.
Can you explain the Java thread lifecycle?
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).