A very simple code that should print numbers from 7 to 21. But does it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.concurrent.TimeUnit;
class Counter extends Thread {
//instance variable
Integer count = 0;
// method where the thread execution will start
public void run() {
int fixed = 6; //local variable
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread().getName() + ": result="
+ performCount(fixed));
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// letβs see how to start the threads
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName() + " is executing..." );
Counter counter = new Counter();
//5 threads
for (int i = 0; i < 5; i++) {
Thread t = new Thread(counter);
t.start();
}
}
//multiple threads can access me concurrently
private int performCount(int fixed) {
return (fixed + ++count);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.concurrent.TimeUnit;
class Counter extends Thread {
//instance variable
Integer count = 0;
// method where the thread execution will start
public void run() {
int fixed = 6; //local variable
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread().getName() + ": result="
+ performCount(fixed));
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// letβs see how to start the threads
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName() + " is executing..." );
Counter counter = new Counter();
//5 threads
for (int i = 0; i < 5; i++) {
Thread t = new Thread(counter);
t.start();
}
}
//multiple threads can access me concurrently
private int performCount(int fixed) {
return (fixed + ++count);
}
}
Q1. What is a thread?
A1. It is a thread of execution in a program. The JVM (i.e. process) allows an application to have multiple threads of execution running concurrently. In the Hotspot JVM there is a direct mapping between a Java Thread and a native operating system (i.e. OS) Thread. When a thread terminates, all resources for both the native and Java thread are released.
Q2. What are the JVM created threads?
A2. The main thread and a number of background threads.
1) main thread, which is created as part of invoking the main(β¦) method
https://cjavapoint.blogspot.com/
A1. It is a thread of execution in a program. The JVM (i.e. process) allows an application to have multiple threads of execution running concurrently. In the Hotspot JVM there is a direct mapping between a Java Thread and a native operating system (i.e. OS) Thread. When a thread terminates, all resources for both the native and Java thread are released.
Q2. What are the JVM created threads?
A2. The main thread and a number of background threads.
1) main thread, which is created as part of invoking the main(β¦) method
https://cjavapoint.blogspot.com/
π1
3) Garbage Collection low priority background thread for minor GC activities. Minor GC evicts the objects in the βEdenβ and βSurvivorβ part of the heap.
4) Compiler background thread to compile byte code to native code at run-time.
5) Other background threads such as signal dispatcher thread and periodic task thread.
Q3. What is the key difference between a process and a thread?
A3. A process is an execution of a program but a thread is a single execution sequence within the process. A process can contain multiple threads. A thread is sometimes called a lightweight process.
4) Compiler background thread to compile byte code to native code at run-time.
5) Other background threads such as signal dispatcher thread and periodic task thread.
Q3. What is the key difference between a process and a thread?
A3. A process is an execution of a program but a thread is a single execution sequence within the process. A process can contain multiple threads. A thread is sometimes called a lightweight process.
Forwarded from java interview Questions
java interview Questions Join Groupππππππ https://t.me/javaQuestions4u
Telegram
java interview Questions
java interview questions for beginners
Any promotion for
rajp6133@gmail.com
Any promotion for
rajp6133@gmail.com
What is method overloading?
Method overloading is a programming technique that allows developers to use the same method name multiple times in the same class, but with different parameters. In this case, we say that the method is overloaded. Listing 1 shows a single method whose parameters differ in number, type, and order.
Listing 1. Three types of method overloading
Number of parameters:
public class Calculator {
void calculate(int number1, int number2) { }
void calculate(int number1, int number2, int number3) { }
}
Type of parameters:
public class Calculator {
void calculate(int number1, int number2) { }
void calculate(double number1, double number2) { }
}
Order of parameters:
public class Calculator {
void calculate(double number1, int number2) { }
void calculate(int number1, double number2) { }
} more java interview Questions visit https://cjavapoint.blogspot.com/ join my me on https://t.me/javaQuestions4u ππππππππ
Method overloading is a programming technique that allows developers to use the same method name multiple times in the same class, but with different parameters. In this case, we say that the method is overloaded. Listing 1 shows a single method whose parameters differ in number, type, and order.
Listing 1. Three types of method overloading
Number of parameters:
public class Calculator {
void calculate(int number1, int number2) { }
void calculate(int number1, int number2, int number3) { }
}
Type of parameters:
public class Calculator {
void calculate(int number1, int number2) { }
void calculate(double number1, double number2) { }
}
Order of parameters:
public class Calculator {
void calculate(double number1, int number2) { }
void calculate(int number1, double number2) { }
} more java interview Questions visit https://cjavapoint.blogspot.com/ join my me on https://t.me/javaQuestions4u ππππππππ
Telegram
java interview Questions
java interview questions for beginners
Any promotion for
rajp6133@gmail.com
Any promotion for
rajp6133@gmail.com
β€1
What are the various access specifiers in Java? In Java, access specifiers are the keywords which are used to define the access scope of the method, class, or a variable. In Java, there are four access specifiers given below.
Public The classes, methods, or variables which are defined as public, can be accessed by any class or method.
Protected Protected can be accessed by the class of the same package, or by the sub-class of this class, or within the same class.
Default Default are accessible within the package only. By default, all the classes, methods, and variables are of default scope.
Private The private class, methods, or variables defined as private can be accessed within the class only. For More Questions Visit :- πππππ https://cjavapoint.blogspot.com/
Public The classes, methods, or variables which are defined as public, can be accessed by any class or method.
Protected Protected can be accessed by the class of the same package, or by the sub-class of this class, or within the same class.
Default Default are accessible within the package only. By default, all the classes, methods, and variables are of default scope.
Private The private class, methods, or variables defined as private can be accessed within the class only. For More Questions Visit :- πππππ https://cjavapoint.blogspot.com/
Check out this post⦠"Composition in Java Example - Learn Java".
http://cjavapoint.blogspot.com/2019/12/composition.html
http://cjavapoint.blogspot.com/2019/12/composition.html
Check out this post⦠"What is data hiding in a program? - Learn Java".
http://cjavapoint.blogspot.com/2019/09/data-hiding.html
http://cjavapoint.blogspot.com/2019/09/data-hiding.html
Core java Interview Questions for All. Aggregation
Learn java interview questions for student.learn java,core java interview question,java interview question.
Without existing container object if there is a chance of existing contained objects
such type of relationship is called aggregation .
in aggregation objects have weak association.
Example:
Within a department there may be a chance of several professors will work whenever
we are closing department still there may be a chance of existing department object the
relationship between department and professor is called aggregation where the objects
having weak association.
More Questions Visit My Blog ππππππππhttps://cjavapoint.blogspot.com/
Learn java interview questions for student.learn java,core java interview question,java interview question.
Without existing container object if there is a chance of existing contained objects
such type of relationship is called aggregation .
in aggregation objects have weak association.
Example:
Within a department there may be a chance of several professors will work whenever
we are closing department still there may be a chance of existing department object the
relationship between department and professor is called aggregation where the objects
having weak association.
More Questions Visit My Blog ππππππππhttps://cjavapoint.blogspot.com/
Exception handling Interview Questions
Q) What is an exception?
Exceptions are abnormal conditions that arise during execution of the program. It may occur due to wrong user input or wrong logic written by programmer.
Q) Exceptions are defined in which java package? OR which package has definitions for all the exception classes?
Java.lang.Exception
This package contains definitions for Exceptions.
https://cjavapoint.blogspot.com/
Q) What is an exception?
Exceptions are abnormal conditions that arise during execution of the program. It may occur due to wrong user input or wrong logic written by programmer.
Q) Exceptions are defined in which java package? OR which package has definitions for all the exception classes?
Java.lang.Exception
This package contains definitions for Exceptions.
https://cjavapoint.blogspot.com/
Q) What is throw keyword in exception handling?
The throw keyword is used for throwing user defined or pre-defined exception.
Q) What is throws keyword?
If a method does not handle a checked exception, the method must declare it using the throwskeyword. The throws keyword appears at the end of a methodβs signature.
https://cjavapoint.blogspot.com/
The throw keyword is used for throwing user defined or pre-defined exception.
Q) What is throws keyword?
If a method does not handle a checked exception, the method must declare it using the throwskeyword. The throws keyword appears at the end of a methodβs signature.
https://cjavapoint.blogspot.com/
Q1) What is the difference between an Inner Class and a Sub-Class?
Ans: An Inner class is a class which is nested within another class. An Inner class has access rights for the class which is nesting it and it can access all variables and methods defined in the outer class.
A sub-class is a class which inherits from another class called super class. Sub-class can access all public and protected methods and fields of its super class
https://cjavapoint.blogspot.com/
Ans: An Inner class is a class which is nested within another class. An Inner class has access rights for the class which is nesting it and it can access all variables and methods defined in the outer class.
A sub-class is a class which inherits from another class called super class. Sub-class can access all public and protected methods and fields of its super class
https://cjavapoint.blogspot.com/
Q2) What are the various access specifiers for Java classes?
Ans: In Java, access specifiers are the keywords used before a class name which defines the access scope. The types of access specifiers for classes are:
1) Public: Class,Method,Field is accessible from anywhere.
2) Protected: Method,Field can be accessed from the same class to which they belong or from the sub-classes, and from the class of same package, but not from outside.
3) Default: Method,Field,class can be accessed only from the same package and not from outside of itβs native package.
4) Private: Method,Field can be accessed from the same class to which they belong.
https://cjavapoint.blogspot.com/
Ans: In Java, access specifiers are the keywords used before a class name which defines the access scope. The types of access specifiers for classes are:
1) Public: Class,Method,Field is accessible from anywhere.
2) Protected: Method,Field can be accessed from the same class to which they belong or from the sub-classes, and from the class of same package, but not from outside.
3) Default: Method,Field,class can be accessed only from the same package and not from outside of itβs native package.
4) Private: Method,Field can be accessed from the same class to which they belong.
https://cjavapoint.blogspot.com/
Q3) Whatβs the purpose of Static methods and static variables?
Ans: When there is a requirement to share a method or a variable between multiple objects of a class instead of creating separate copies for each object, we use static keyword to make a method or variable shared for all objects
https://cjavapoint.blogspot.com/
Ans: When there is a requirement to share a method or a variable between multiple objects of a class instead of creating separate copies for each object, we use static keyword to make a method or variable shared for all objects
https://cjavapoint.blogspot.com/
Q4) What is data encapsulation and whatβs its significance?
Ans: Encapsulation is a concept in Object Oriented Programming for combining properties and methods in a single unit.
Encapsulation helps programmers to follow a modular approach for software development as each object has its own set of methods and variables and serves its functions independent of other objects. Encapsulation also serves data hiding purpose.
https://cjavapoint.blogspot.com/
Ans: Encapsulation is a concept in Object Oriented Programming for combining properties and methods in a single unit.
Encapsulation helps programmers to follow a modular approach for software development as each object has its own set of methods and variables and serves its functions independent of other objects. Encapsulation also serves data hiding purpose.
https://cjavapoint.blogspot.com/