interview sat
interview sat
2> Non primitive data types :can created by programmer . They are
String, Classes, Arrays
Byte -> is a key word is used to declare a variable that can hold 8 bit data values.
Short -> is a key word is used to declare a variable that can hold 16 bit data values, it means it
can hold byte data type.
Int -> is a key word is used to declare a variable that can hold 36 bit data values , it means it can
hold “short” and “byte” data type.
Long -> is a key word is used to declare a variable that can hold 64 bit data values, it means it
can hold “byte”, “short”, “int” data types.
Float -> is a key word in java used to declare a variable that can hold a 32 bit number and it
could accept float point.
Char -> if we have to hold any single character then we can use this char data type.
Flow controls :
Def: control flow statements let you control the flow of the execution of the code
in your program . In java programming language, you can control the flow of
execution of the by placing the decision making, branching , looping and adding
conditional blocks.
Control flow statements are :
2. looping statements
For loop
While loop
Do-while loop
3.branching statements
Break statements
Continue statements
Return statements
<conditional statement>
Def: In Java, the main conditional statements are if, else if, else, and the switch statement.
1. if Statement: The if statement is used to execute a block of code only if a specified condition
is true. It has the following
syntax:
if (condition) {
// code to be executed if the condition is true
}
Example:
int x = 10;
if (x > 5) {
System.out.println("x is greater than 5");
}
2. if-else Statement: The if-else statement allows you to specify two blocks of code—one to be
executed if the condition is true, and another if the condition is false. It has the following
syntax:
if (condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}
Example:
int x = 3;
if (x > 5) {
System.out.println("x is greater than 5");
} else {
System.out.println("x is not greater than 5");
}
3. if-else if-else Statement: The if-else if-else statement allows you to specify
multiple conditions and execute different blocks of code based on the first true
condition. It has the following
syntax:
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition2 is true
} else {
// code to be executed if none of the conditions are true
}
Example:
int x = 7;
if (x > 10) {
System.out.println("x is greater than 10");
} else if (x > 5) {
System.out.println("x is greater than 5 but not 10");
} else {
System.out.println("x is 5 or less");
}
4. switch Statement: The switch statement is used to select one of many code blocks to be
executed. It evaluates an expression and compares it with the values of different case labels. It
has the following syntax:
switch (expression) {
case value1:
// code to be executed if expression == value1
break;
case value2:
// code to be executed if expression == value2
break;
// ... more cases ...
default:
// code to be executed if none of the cases match
}
Example:
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
// ... more cases ...
default:
System.out.println("Invalid day");
}
// code to be executed }
2. while Loop: The while loop is used when the number of iterations is not known
in advance, and the loop continues as long as a specified condition is true. It has
the following syntax:
while (condition) {
// code to be executed
}
Example :
int i = 0;
while (i < 5) {
System.out.println("Iteration: " + i);
i++;
\\here the code will execute int 0 to 4}
3. do-while Loop: The do-while loop is similar to the while loop, but it ensures
that the block of code is executed at least once, as the condition is checked after
the execution of the loop. It has the following syntax:
do {
// code to be executed
} while (condition);
Example :
int i = 0;
do {
System.out.println("Iteration: " + i);
i++;
} while (i < 5); \\ this loop execute the block code first and later checks the
condition
>Arrays
In Java, an array is a data structure that allows you to store multiple values of the
same data type under a single variable name.
>object
In object-oriented programming (OOP), an object is an instance of a class. A class
is a blueprint or template that defines the attributes (fields or properties) and
behaviors (methods) that objects of that type will have. Objects are created from
classes and represent real-world entities or concepts in a program.
>class
A class is a user-defined data type that defines a blueprint for objects. It
encapsulates data (attributes or fields) and the methods (functions or behaviors)
that operate on that data.
>instance variables
Return Type:
The return type specifies the type of data that the method will return. If a method
does not return any value, the return type is specified as void.
Parameters:
Parameters are variables that are passed into a method. They allow methods to
receive input values when called.
Method Invocation:
To execute a method, you invoke or call it. The method is called by its name, and
if it has parameters, you pass values or variables as arguments.
>Method Overloading:
Method overloading allows a class to have multiple methods with the same name
but different parameter lists (number or types of parameters). The compiler
determines which method to call based on the method signature.
>method overloading
>Static Methods:
Static methods belong to the class rather than an instance of the class. They are
called using the class name and can be invoked without creating an object.
\\Static method
public static void staticMethod() {
System.out.println("This is a static method.");
}
>Void Methods:
A method with a return type of void does not return any value. It performs an
action or task without producing a result.
// Void method
public void printHello() {
System.out.println("Hello, World!");
}
>static block
The static block is executed when the class is loaded into the Java Virtual Machine
(JVM) and is executed only once, regardless of the number of instances created or
the number of times the class is referenced. It provides a way to perform static
initialization before any other code in the class is executed.
Here is the syntax for a static block:
// Static block
static {
// Code for static initialization
staticVariable = 42;
System.out.println("Static block executed");
}
>instance block
Instance blocks are also known as instance initializers, and they are used for
performing initialization tasks for instance variables when an object of the class is
created. Each time an object is instantiated, the instance block is executed before
the constructor.
Here is the syntax for an instance block:
public class MyClass {
// Instance variable
int instanceVariable;
// Instance block
{
// Code for instance initialization
instanceVariable = 10;
System.out.println("Instance block executed");
}
// Constructor
public MyClass() {
// Code for the constructor
System.out.println("Constructor executed");
}
>encapsulation
Def: encapsulation is hiding data behind the setter and getter methods it consists
a private variables too
Getter Methods:
Getter methods, also known as accessor methods, are used to retrieve the
values of private attributes. They provide read-only access to the attributes.
public class MyClass {
private int myAttribute;
>constructor
>INHERITANCE
Inheritance is one of the four fundamental principles of object-oriented
programming (OOP) and is a mechanism that allows a new class (subclass or
derived class) to inherit properties and behaviors from an existing class
(superclass or base class). Inheritance promotes code reuse and the creation of a
hierarchical relationship among classes, where a subclass can extend or specialize
the functionality of its superclass.
// Main class
public class Main {
public static void main(String[] args) {
// Creating an object of the subclass
Dog myDog = new Dog();
EXAMPLE:
// Superclass
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
System.out.println("Dog is barking");
}
}
// Main class
public class Main {
>hierarchical inheritance
Hierarchical inheritance is a type of inheritance in object-oriented programming
where a single base class (superclass) is extended by multiple derived classes
(subclasses). In this scenario, each subclass inherits from the same superclass,
creating a hierarchical structure or tree-like relationship.
EXAMPLE:
// Superclass
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
}
}
// Main class
public class Main {
public static void main(String[] args) {
// Creating objects of the subclasses
Cat myCat = new Cat();
>MULTIPLE INHERITANCE
Multiple inheritance is a feature in object-oriented programming that allows a
class to inherit properties and behaviors from more than one class. In multiple
inheritance, a class can have more than one direct superclass, and it inherits
attributes and methods from all of them. While multiple inheritance can be a
powerful tool for code reuse, it can also introduce complexities and challenges,
such as the diamond problem.
EXAMPLE:
WITH CLASS:
// First superclass
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
// Second superclass
class Mammal {
void breathe() {
System.out.println("Mammal is breathing");
}
}
// Second interface
interface Mammal {
void breathe();
}
@Override
public void breathe() {
System.out.println("Dog is breathing");
}
void bark() {
System.out.println("Dog is barking");
}
}
>HYBRID
Hybrid inheritance is a combination of two or more types of inheritance within a
single program. It typically involves a mix of different types of inheritance, such as
single inheritance, multiple inheritance (through interfaces), hierarchical
inheritance, or multilevel inheritance. Hybrid inheritance is a way to take
advantage of the benefits of various inheritance types to model relationships in a
more flexible manner.
EXAMPLE:
// Superclass
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
// Interface 1
interface Mammal {
void breathe();
}
// Interface 2
interface Pet {
void play();
}
@Override
public void play() {
System.out.println("Dog is playing");
}
void bark() {
System.out.println("Dog is barking");
}
}
// Main class
public class Main {
public static void main(String[] args) {
// Creating an object of the subclass
Dog myDog = new Dog();
>POLYMORPHYSM
DEF:
Polymorphism is one of the four fundamental principles of object-oriented
programming (OOP) and refers to the ability of a single entity (such as a method,
operator, or object) to take on multiple forms. There are two main types of
polymorphism in Java: “compile-time (or static) polymorphism” and “runtime (or
dynamic) polymorphism.”
DEF:
Runtime polymorphism is also known as method overriding. It occurs when a
subclass provides a specific implementation for a method that is already defined
in its superclass.
The decision about which method to call is made at runtime, based on the actual
type of the object.
EXAMPLE:
// Superclass
class Animal {
public void makeSound() {
System.out.println("Generic animal sound");
}
}
In this example, both Circle and Square implement the Shape interface, and
objects of these classes can be treated as instances of the Shape interface.
>ACCESS MODIFIERS
Access modifiers in Java are keywords that determine the visibility or accessibility
of classes, fields, methods, and other members within a Java program. These
modifiers control the level of access that other classes or components have to the
declared elements. There are four main access modifiers in Java:
public:
The public modifier allows unrestricted access to the associated class, method, or
field. Members marked as public can be accessed from any other class in the
same program or even from external programs.
Example: public class MyClass { ... }, public void myMethod() { ... }
private:
The private modifier restricts access to the associated class, method, or field to
only within the same class. It provides the highest level of encapsulation, ensuring
that the member is not accessible from outside the class.
Example: private int myField;, private void myMethod() { ... }
protected:
The protected modifier allows access to the associated class, method, or field
within the same package and by subclasses, regardless of the package. It provides
a level of access between public and private.
Example: protected int myField;, protected void myMethod() { ... }
default (Package-Private):
If no access modifier is specified (also known as package-private or default), the
member is accessible only within the same package. It provides a level of access
between public and private and is the default access level if no modifier is
specified.
Example: int myField;, void myMethod() { ... }
>ABSTRACT
Abstract Class:
An abstract class in Java is a class that cannot be instantiated on its own and is
meant to be subclassed by other classes. It may contain both abstract methods
(methods without a body) and concrete methods (methods with an
implementation). Abstract classes are typically used as base classes in inheritance
hierarchies.
The abstract keyword is used to declare an abstract class.
Example:
abstract class Shape {
// Abstract method (no implementation)
abstract void draw();