1). What is Java?
Java is a high-level, general-purpose, memory-safe, object-oriented programming language. It is intended to let programmers write once, run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need to recompile.
2). Features of Java?
- Platform Independent
- Multithreaded
- Simple
- Robust
- Architecture Neutral
3). Difference between JDK, JRE and JVM.
Java Development Kit, or JDK, is a software development kit that is a superset of JRE. It is the foundational component that enables Java application and Java applet development. It is platform-specific, so separate installers are needed for each operating system (for example, Mac, Unix, and Windows).
Java Runtime Environment, or JRE, is a set of software tools responsible for execution of the Java program or application on your system.
Java virtual machine, or JVM, loads, verifies, and runs Java bytecode. It is known as the interpreter or the core of the Java programming language because it runs Java programming.
javac – the Java compiler, which converts source code into Java bytecode - complier time
java - Byte code to Binary code convert with help of JRE - Run time
*4). What is Class? *
A class is the blueprint from which individual objects are created.
5). What is Object?
A Java object is an instance of a class. (Or) Physical entity
6). How do you create Object in Java?
Class_name Reference_name = new Class_name();
7). What is Method?
In Java, a method is a block of code that is a type of encapsulation. The method has been written to execute a certain task every time it is called by the user. Verb/Task
8). How do you pass arguments to methods?
All arguments to methods in Java are pass-by-value.
// Method definition
public int mult(int x, int y){ // parameter
return x * y;
}
// Where the method mult is used
int length = 10;
int width = 5;
int area = mult(length, width); // argument
9). How do you call methods?
object_name.method_name(argument1, argument2)
example: int area = mult(length, width);
10). Difference between static and non-static methods
class Calculator {
// Static method (class-level)
static int add(int a, int b) {
return a + b;
}
// Non-static method (instance-level)
int multiply(int a, int b) {
return a * b;
}
}
public class Main {
public static void main(String[] args) {
// Static method call (no object needed)
System.out.println(Calculator.add(2, 3)); // Output: 5
// Non-static method call (requires object)
Calculator calc = new Calculator();
System.out.println(calc.multiply(2, 3)); // Output: 6
}
}
Static Method: **
Can Access from Static methods only, Class level, Access using class name.
Calling - Calculator.add(2, 3)
**Non-Static Method:
Access from any methods, Should access only via instances.
Calling - calc.multiply(2, 3)
11). Difference between static and non-static variables
Static variables as global variable
Non-static variables changed based on the objects
Calling: Class_name.Threatre_owner
Calling: Object_name.ticket_rate = 100;
12). Difference between Global and Local Variables
Global - Can access entire class level, initial values by default
Local - Only method level, initial values must be assign
13). What is Polymorphism?
In object-oriented programming, polymorphism is the provision of one interface to entities of different data types.
14). Types of Polymorphism?
Method Overloading or Compile time Polymorphism
Based on the parameters methods will call
Method Overriding or Run time Polymorphism:
Based on the order method will call by object
15). What is Method Overloading?
Multiple methods can have same name but parameters will decide.
16). What is Encapsulation?
Encapsulation is a key concept in OOP. This concept involves combining data and the methods that operate on it into one unit, usually a class. Encapsulation protects data from accidental modification, enhances code organization, and streamlines interaction between program components.
*17). Access Modifiers *
These keywords determine the accessibility of classes, methods, and other members. They are part of the programming language syntax that allows for component encapsulation.
Default, Private, Protected, Public
18). Difference between private and default
Default: Omitting an access modifier for a class, method, or data member grants access only within the current package.
Private: The private keyword declares members accessible only within the declaring class. Other classes in the same program cannot access these private methods or data members. Classes and interfaces cannot be private.
19). Difference between private and public
The public access modifier is declared using the keyword public. It has the widest scope among all other access modifiers. Classes and methods that are declared public can be accessed from anywhere in your code. There are no restrictions on public data members.
20). What is Inheritance?
Inheritance is a concept using which a class can inherit the properties of another class. Inheritance allows the creation of hierarchal classifications and therefore is considered as one of the cornerstones of object-oriented programming.
21) How do you achieve Inheritance in Java?
In Java, inheritance is achieved using the extends keyword. A subclass (or child class) can extend a superclass (or parent class) to inherit its attributes and methods. This means the subclass gets access to the superclass's members (except private ones) and can add its own unique features.
22). How do you create Constants in Java? [final keyword]
public final int ticket = 200;
The reason why global constants in Java use the static and final keywords is because final ensures a variable cannot change, while static ensures only one copy of the constant variable is placed in memory, regardless of how many class instances are created.
23). What is return data type?
Return type may be a primitive data type like int, float, double, a reference type, or void type which represents “return nothing”. That is, they don’t give anything back. When a method is called, the method may return a value to the caller method.
24). Why do we need getter methods?
Getter methods (also called accessors) are used to safely access the private variables of a class from outside that class.
25). Explain me public static void main(String[] args).
Public - Can access the method anywhere.
static - we can access this in the class without object
Void - Return datatype
main - method name
String - Non-Primite datatype return
Programme use to starts to excuate from here. this is signature of the class.
26). Can we overload main method?
Yes, We can overload the main method.
27). Can we change return datatype of main method?
We can change but this program won't run.
28). Can we have private classes?
We can't have the private classes.
29). Can we add Access Modifiers to local variables?
No, you cannot add access modifiers.
Class, package or Inheritance level only it will work.
30). Can we run classes without main method? If not, Why?
We can't run the class with main method. but it will access as super class.
31). Which classes can not be extended?
if class mentioned as final key can't be extended
32). Why Polymorphism is needed?
In the context of programming, polymorphism enables the same function to be called with different signatures, allowing for flexibility and adaptability in code design.
33). Why Encapsulation is needed?
Encapsulation prevents people who work on your code from making mistakes, by making sure that they only access things they're supposed to access.
-------------- No, AI Assistance used here ---------------
Top comments (4)
likes the last line :)
This can be used for me as a cheat sheet, nice bro !
Nice work helpful for us bro
Thank you.