1. Explain different operators in java with example programs
Java provides many types of operators which can be used according to the need. They are classified based on the functionality they provide. Some of the types are:
- Arithmetic Operators
- Unary Operators
- Assignment Operator
- Relational Operators
- Logical Operators
- Ternary Operator
- Bitwise Operators
- Shift Operator
Let’s take a look at them in detail.
1. Arithmetic Operators: They are used to perform simple arithmetic operations on primitive data types.
- * : Multiplication
- / : Division
- % : Modulo
- + : Addition
- – : Subtraction
2. Unary Operators: Unary operators need only one operand. They are used to increment, decrement or negate a value.
- ++ : Increment operator,
- Post-Increment:
- Pre-Increment:
- — : Decrement operator,
- Post-decrement:
- Pre-Decrement:
3. Assignment Operator: ‘=’ Assignment operator is used to assign a value to any variable.
4. Relational Operators: These operators are used to check for relations like equality, greater than, less than. They return boolean results after the comparison and are extensively used in looping statements as well as conditional if-else statements. The general format is,
- Some of the relational operators are-
- ==,.
- !=,
- <,\
- <=,
- >,
- >=,
5. Logical Operators: These operators are used to perform “logical AND” and “logical OR” operations, i.e., the function similar to AND gate and OR gate in digital electronics. One thing to keep in mind is the second condition is not evaluated if the first one is false, i.e., it has a short-circuiting effect. Used extensively to test for several conditions for making a decision. Java also have “Logical NOT”, it returns true when condition is false and vice-versa
Conditional operators are:
- &&, Logical AND: returns true when both conditions are true.
- ||, Logical OR: returns true if at least one condition is true.
- ! , Logical NOT: returns true when condition is false and vice-versa
6. Ternary operator: Ternary operator is a shorthand version of the if-else statement. It has three operands and hence the name ternary.
The general format is:
condition ? if true : if false
7. Bitwise Operators: These operators are used to perform the manipulation of individual bits of a number. They can be used with any of the integer types. They are used when performing update and query operations of the Binary indexed trees.
- &, Bitwise AND operator: returns bit by bit AND of input values.
- |, Bitwise OR operator: returns bit by bit OR of input values.
- ^, Bitwise XOR operator: returns bit by bit XOR of input values.
- ~, Bitwise Complement Operator: This is a unary operator which returns the one’s complement representation of the input value, i.e., with all bits inverted.
8. Shift Operators: These operators are used to shift the bits of a number left or right, thereby multiplying or dividing the number by two, respectively. They can be used when we have to multiply or divide a number by two. General format-
number shift_op number_of_places_to_shift;
- <<, Left shift operator: shifts the bits of the number to the left and fills 0 on voids left as a result. Similar effect as of multiplying the number with some power of two.
- >>, Signed Right shift operator: shifts the bits of the number to the right and fills 0 on voids left as a result. The leftmost bit depends on the sign of the initial number. Similar effect as of dividing the number with some power of two.
A constructor is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes.
The constructor overloading can be defined as the concept of having more than one constructor with different parameters so that every constructor can perform a different task.
Example
Output:
this a default constructor Default Constructor values: Student Id : 0 Student Name : null Parameterized Constructor values: Student Id : 10 Student Name : David
3.Write a java program to print the names of students by creating a Student class. If no name is passed while creating an object of Student class, then the name should be "Unknown", otherwise the name should be equal to the String value passed while creating object of Student class.
Class student {
Private string name;
Student(string n){
name=n
System.out.println(name);
}
Student( ){
System.out.println(“unknown “);
}
Class Name{
PublicStaticVoid main(String arg()){
Student w =new student ( );
Student e =new student (“chaduvukondifirst” );
}
}
Output
Unknown
Chaduvukondifirst
4 .how super keyword is used
The super
keyword refers to superclass (parent) objects.
It is used to call superclass methods, and to access the superclass constructor.
The most common use of the super
keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.
class Animal { // Superclass (parent)
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal { // Subclass (child)
public void animalSound() {
super.animalSound(); // Call the superclass method
System.out.println("The dog says: bow wow");
}
}
public class Main {
public static void main(String args[]) {
Animal myDog = new Dog(); // Create a Dog object
myDog.animalSound(); // Call the method on the Dog object
}
}
Outputs : The animal makes a sound The dog says: bow wow
THEthis
keyword refers to the current object in a method or constructor.
public class Main {
int x;
// Constructor with a parameter
public Main(int x) {
this.x = x;
}
// Call the constructor
public static void main(String[] args) {
Main myObj = new Main(5);
System.out.println("Value of x = " + myObj.x);
}
}output
Value of x = 5
6 .Explain Arrays class in java with example program
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). The base value is index 0 and the difference between the two indexes is the offset.
An array can contain primitives (int, char, etc.) and object (or non-primitive) references of a class depending on the definition of the array. In the case of primitive data types, the actual values are stored in contiguous memory locations. example :7 .Explain different control statements in java with example programs
three types of control flow statements.
- Decision Making statements
- if statements
- switch statement
- Loop statements
- do while loop
- while loop
- for loop
- for-each loop
- Jump statements
- break statement
- continue statement
Decision-Making statements:
As the name suggests, decision-making statements decide which statement to execute and when. Decision-making statements evaluate the Boolean expression and control the program flow depending upon the result of the condition provided. There are two types of decision-making statements in Java, i.e., If statement and switch statement.
1) If Statement:
In Java, the "if" statement is used to evaluate a condition. The control of the program is diverted depending upon the specific condition. The condition of the If statement gives a Boolean value, either true or false. In Java, there are four types of if-statements given below.
- Simple if statement
- if-else statement
- if-else-if ladder
- Nested if-statement
Let's understand the if-statements one by one.
1) Simple if statement:
It is the most basic statement among all control flow statements in Java. It evaluates a Boolean expression and enables the program to enter a block of code if the expression evaluates to true.
Syntax of if statement is given below.
2) if-else statement
The if-else statement is an extension to the if-statement, which uses another block of code, i.e., else block. The else block is executed if the condition of the if-block is evaluated as false.
Syntax:
3) if-else-if ladder:
The if-else-if statement contains the if-statement followed by multiple else-if statements. In other words, we can say that it is the chain of if-else statements that create a decision tree where the program may enter in the block of code where the condition is true. We can also define an else statement at the end of the chain.
Syntax of if-else-if statement is given below.
4. Nested if-statement
In nested if-statements, the if statement can contain a if or if-else statement inside another if or else-if statement.
Syntax of Nested if-statement is given below.
- switch (expression){
- case value1:
- statement1;
- break;
- .
- .
- .
- case valueN:
- statementN;
- break;
- default:
- default statement;
- }
Loop Statements
we need to execute the block of code repeatedly while some condition evaluates to true. However, loop statements are used to execute the set of instructions in a repeated order. The execution of the set of instructions depends upon a particular condition.
In Java, we have three types of loops that execute similarly. However, there are differences in their syntax and condition checking time.
- for loop
- while loop
- do-while loop
Let's understand the loop statements one by one.
for loop
In Java, for loop is similar to C and C++. It enables us to initialize the loop variable, check the condition, and increment/decrement in a single line of code. We use the for loop only when we exactly know the number of times, we want to execute the block of code.
The flow chart for the for-loop is given below.

for-each loop
Java provides an enhanced for loop to traverse the data structures like array or collection. In the for-each loop, we don't need to update the loop variable. The syntax to use the for-each loop in java is given below.
while loop
The while loop is also used to iterate over the number of statements multiple times. However, if we don't know the number of iterations in advance, it is recommended to use a while loop. Unlike for loop, the initialization and increment/decrement doesn't take place inside the loop statement in while loop.
It is also known as the entry-controlled loop since the condition is checked at the start of the loop. If the condition is true, then the loop body will be executed; otherwise, the statements after the loop will be executed.
The syntax of the while loop is given below.
The flow chart for the while loop is given in the following image.

do-while loop
The do-while loop checks the condition at the end of the loop after executing the loop statements. When the number of iteration is not known and we have to execute the loop at least once, we can use do-while loop.
It is also known as the exit-controlled loop since the condition is not checked in advance. The syntax of the do-while loop is given below.
8 .Explain method overloading in java with example program
- class Adder{
- static int add(int a,int b){return a+b;}
- static int add(int a,int b,int c){return a+b+c;}
- }
- class TestOverloading1{
- public static void main(String[] args){
- System.out.println(Adder.add(11,11));
- System.out.println(Adder.add(11,11,11));
- }}
output:
22 33
9.Create a class named 'PrintNumber' to print various numbers of different datatypes by creating different methods with the same name 'printdata' having a parameter for each datatype
class Number{
int printdata(int a){return a;}
float printdata(float a){return a;}
double printdata(double a){return a;}
}
public class PrintNumber
{
int a;
float b;
double c;
public static void main(String[] args) {
Number c1= new Number();
System.out.println(c1.printdata(5));
System.out.println(c1.printdata(6.00));
System.out.println(c1.printdata(787787));
}
}
Ouput :
5
6.00
7878787
10 .Explain types of inheritances in javaTypes of Inheritance
- Single Inheritance
In single inheritance, a sub-class is derived from only one super class. It inherits the properties and behavior of a single-parent class. Sometimes it is also known as simple inheritance.

.
Executive.java
Output:
Total salary credited: 414408.0 Bonus of six months: 18000.0
- Multi-level Inheritance
In multi-level inheritance, a class is derived from a class which is also derived from another class is called multi-level inheritance. In simple words, we can say that a class that has more than one parent class is called multi-level inheritance. Note that the classes must be at different levels. Hence, there exists a single base class and single derived class but multiple intermediate base classes.

MultilevelInheritanceExample.java
Output:
registration number= 0987 marks= 78.0 score= 68.7
- Hierarchical Inheritance
Hierarchical Inheritance
If a number of classes are derived from a single base class, it is called hierarchical inheritance.

HierarchicalInheritanceExample.java
Output:
The method of the class Student invoked. The method of the class Student invoked. The method of the class Student invoked.
- Hybrid Inheritance
Hybrid Inheritance
Hybrid means consist of more than one. Hybrid inheritance is the combination of two or more types of inheritance.

Daughter.java
Output:
I am daughter.