ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY
E-SECTION
JAVA Q AND A FOR IA 2
1) Explain arithmetic operator with example.
Ans:
Arithmetic operators are used in mathematical expressions in the same way that they are used
in algebra. The following table lists the arithmetic operators:
Operator Result
+ Addition
– Subtraction (also unary minus)
* Multiplication
/ Division
% Modulus
++ Increment
+= Addition assignment
–= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
–– Decrement
Example :
class BasicMath
{
public static void main(String args[])
{
// arithmetic using integers
System.out.println("Integer Arithmetic");
int a = 1 + 1; int b = a * 3;
int c = b / 4;
int d = c - a;
int e = -d;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("e = " + e);
// arithmetic using doubles
System.out.println("\nFloating Point Arithmetic");
double da = 1 + 1;
double db = da * 3;
double dc = db / 4;
ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY
E-SECTION
JAVA Q AND A FOR IA 2
double dd = dc - a;
double de = -dd;
System.out.println("da = " + da);
System.out.println("db = " + db);
System.out.println("dc = " + dc);
System.out.println("dd = " + dd);
System.out.println("de = " + de);
}
}
OUTPUT :
Integer Arithmetic
a=2
b=6
c=1
d = -1
e=1
Floating Point Arithmetic
da = 2.0
db = 6.0
dc = 1.5
dd = -0.5
de = 0.5
2) Write a program to find the biggest among 3 numbers using ternary operator(syntax also).
Ans:
import java.util.Scanner;
public class BiggestOfThree {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input three numbers
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
System.out.print("Enter the third number: ");
int num3 = scanner.nextInt();
// Syntax of ternary operator: condition ? value_if_true : value_if_false;
int largest = (num1 > num2)
? (num1 > num3 ? num1 : num3)
ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY
E-SECTION
JAVA Q AND A FOR IA 2
: (num2 > num3 ? num2 : num3);
// Output the result
System.out.println("The largest number is: " + largest);
scanner.close();
}
}
Explanation of the Ternary Operator Syntax:
Syntax: condition ? expression1 : expression2;
o condition: A boolean expression.
o expression1: The value or expression returned if the condition is true.
o expression2: The value or expression returned if the condition is false.
INPUT :
Enter the first number: 45
Enter the second number: 78
Enter the third number: 23
OUTPUT :
The largest number is: 78
3) List and explain relational operators.
Ans:
• Relational Operators
• The relational operators determine the relationship that one operand has to the other.
Specifically, they determine equality and ordering. The relational operators are :
ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY
E-SECTION
JAVA Q AND A FOR IA 2
• The outcome of these operations is a boolean value. The relational operators are most
frequently used in the expressions that control the if statement and the various loop
statements.
• only integer, floating-point, and character operands may be compared to see which is
greater or less than the other.
int a = 4;
int b = 1;
boolean c = a < b;
4) Explain the following operators with example
i)And(&)
ii)Or(|)
iii)Right shift(>>)
iv)Left Shift(<<)
Ans:
i) The Bitwise AND :
The AND operator, &, produces a 1 bit if both operands are also 1. A zero is produced
in all other cases.
Here is an example:
00101010 42
& 00001111 15
00001010 10
ii) The Bitwise OR :
The OR operator, |, combines bits such that if either of the bits in the operands is a 1,
then the resultant bit is a 1, as shown here:
00101010 42
| 00001111 15
00101111 47
iii) The Right Shift :
ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY
E-SECTION
JAVA Q AND A FOR IA 2
The right shift operator, >>, shifts all of the bits in a value to the right a specified
number of times. Its general form is :
value >> num
public class RightShiftExample {
public static void main(String[] args) {
int a = 8; // Binary: 1000
int result = a >> 2; // Shift right by 2 positions
System.out.println("Result of 8 >> 2: " + result); // Output: 2 (Binary: 0010)
}
}
iv) The Left Shift :
The left shift operator, <<, shifts all of the bits in a value to the left a specified number
of times.
It has this general form:
value << num
*public class LeftShiftExample {
public static void main(String[] args) {
int a = 3; // Binary: 0011
int result = a << 2; // Shift left by 2 positions
System.out.println("Result of 3 << 2: " + result); // Output: 12 (Binary: 1100)
}
}
5) Explain Boolean logical operators with an example.
Ans :
Boolean Logical Operators
• The Boolean logical operators shown here operate only on boolean operands. All of the
binary logical operators combine two boolean values to form a resultant boolean value.
ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY
E-SECTION
JAVA Q AND A FOR IA 2
• The logical Boolean operators, &, |, and ^, operate on boolean values in the same
way that they operate on the bits of an integer. The logical ! operator inverts the Boolean
state: !true == false and !false == true. The following table shows the effect of each logical
operation:
// Demonstrate the .
class BoolLogic
{
public static void main(String args[])
{
boolean a = true;
boolean b = false;
boolean c = a | b;
boolean d = a & b;
boolean e = a ^ b;
boolean f = (!a & b) | (a & !b);
boolean g = !a;
System.out.println(" a = " + a);
System.out.println(" b = " + b);
System.out.println(" a|b = " + c);
System.out.println(" a&b = " + d);
ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY
E-SECTION
JAVA Q AND A FOR IA 2
System.out.println(" a^b = " + e);
System.out.println("!a&b|a&!b = " + f);
System.out.println(" !a = " + g);
}
}
OUTPUT :
a = true
b = false
a|b = true
a&b = false
a^b = true
!a&b|a&!b = true
!a = false
6) Explain while and do while loop with an example.
Ans :
while :
The while loop is Java’s most fundamental loop statement. It repeats a statement or block
while its controlling expression is true.
its general form:
while(condition) {
// body of loop
}
public class WhileLoopExample {
public static void main(String[] args) {
int count = 1;
System.out.println("While loop output:");
while (count <= 5) {
System.out.println("Count: " + count);
count++; // Increment the count
}
}
}
Output:
ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY
E-SECTION
JAVA Q AND A FOR IA 2
While loop output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
do-while :
The do-while loop always executes its body at least once, because its conditional expression is
at the bottom of the loop. Its general form is :
do {
// body of loop
}
while (condition);
public class DoWhileLoopExample {
public static void main(String[] args) {
int count = 1;
System.out.println("Do-while loop output:");
do {
System.out.println("Count: " + count);
count++; // Increment the count
} while (count <= 5);
}
}
Do-while loop output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
7) Explain 4 different types of if statement in java.
Ans :
1. Simple if Statement
Executes a block of code if the condition is true.
Syntax:
if (condition) {
ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY
E-SECTION
JAVA Q AND A FOR IA 2
// Code to execute if condition is true
}
Example:
public class IfExample {
public static void main(String[] args) {
int age = 18;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
}
}
2. if-else Statement
Provides an alternative block of code to execute if the condition is false.
Syntax
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
Example :
public class IfElseExample {
public static void main(String[] args) {
int number = 10;
if (number % 2 == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
}
}
3. if-else-if Ladder
Used to test multiple conditions sequentially. Once a condition evaluates to true, its
corresponding block is executed, and the remaining conditions are skipped.
Syntax
if (condition1) {
// Code for condition1
} else if (condition2) {
// Code for condition2
} else if (condition3) {
ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY
E-SECTION
JAVA Q AND A FOR IA 2
// Code for condition3
} else {
// Code if none of the above conditions are true
}
Example :
public class IfElseIfExample {
public static void main(String[] args) {
int marks = 85;
if (marks >= 90) {
System.out.println("Grade: A");
} else if (marks >= 75) {
System.out.println("Grade: B");
} else if (marks >= 50) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: F");
}
}
}
4. Nested if Statement
Involves placing an if statement inside another if statement. It is used when conditions are
dependent on one another.
Syntax:
if (condition1) {
if (condition2) {
// Code to execute if both condition1 and condition2 are true
}
}
Example :
public class NestedIfExample {
public static void main(String[] args) {
int age = 20;
int weight = 55;
if (age >= 18) {
if (weight >= 50) {
System.out.println("You are eligible to donate blood.");
}
}
}
ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY
E-SECTION
JAVA Q AND A FOR IA 2
}
8) Explain the switch statement with the program.
Ans:
switch Statement:
The switch statement in Java is used to execute one block of code among many alternatives,
based on the value of a variable or expression. It's an alternative to the if-else-if ladder when
multiple conditions are checked for equality against different values.
Syntax of switch Statement:
switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
// More cases...
default:
// Code to execute if none of the cases match
Example :
import java.util.Scanner;
public class SwitchExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt user for input
System.out.print("Enter a number (1-7) for the day of the week: ");
int day = scanner.nextInt();
ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY
E-SECTION
JAVA Q AND A FOR IA 2
// Switch statement to determine the day
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid input! Please enter a number between 1 and 7.");
scanner.close();
}
ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY
E-SECTION
JAVA Q AND A FOR IA 2
}
9) Explain iteration statement with syntax and example.
Ans:
Iteration statements (also known as loops) in Java allow you to execute a block of code
repeatedly based on a condition. Java provides three main iteration constructs: for, while, and
do-while.
1. for Loop :
Used when the number of iterations is known beforehand.
Syntax:
for (initialization; condition; update) {
// Code to be executed
}
Example:
public class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration: " + i);
}
}
}
2. while Loop :
Used when the number of iterations is not known beforehand and depends on a condition.
Syntax:
while (condition) {
// Code to be executed
}
Example :
public class WhileLoopExample {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println("Iteration: " + i);
i++;
}
}
}
3. do-while Loop :
ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY
E-SECTION
JAVA Q AND A FOR IA 2
Similar to the while loop, but the condition is checked after the loop body. This ensures the
loop executes at least once.
Syntax:
do {
// Code to be executed
} while (condition);
Example:
public class DoWhileLoopExample {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("Iteration: " + i);
i++;
} while (i <= 5);
}
}
10) Explain jump statement with an example.
Ans:
Jump statements are used to transfer control to another part of the program. Java provides
three main types of jump statements: break, continue, and return.
1. break Statement
Purpose: Used to terminate a loop or switch statement prematurely.
Syntax:
break;
Example :
public class BreakExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
System.out.println("Breaking the loop at i = " + i);
break; // Exit the loop
}
System.out.println("i = " + i);
}
}
}
ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY
E-SECTION
JAVA Q AND A FOR IA 2
OUTPUT:
i=1
i=2
i=3
i=4
Breaking the loop at i = 5
2. continue Statement
Purpose: Skips the current iteration of a loop and moves to the next iteration.
Syntax:
continue;
Example:
public class ContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i % 2 == 0) {
continue; // Skip the rest of the loop body for even numbers
}
System.out.println("i = " + i);
}
}
}
OUTPUT:
i=1
i=3
i=5
3. return Statement
Purpose: Exits from the current method and optionally returns a value to the method
caller.
Syntax:
return; // For methods with no return type (void)
return value; // For methods with a return type
Exampel :
public class ReturnExample {
public static void main(String[] args) {
System.out.println("Sum: " + addNumbers(10, 20));
}
public static int addNumbers(int a, int b) {
ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY
E-SECTION
JAVA Q AND A FOR IA 2
if (a < 0 || b < 0) {
return 0; // Return 0 if any number is negative
}
return a + b; // Return the sum if both numbers are positive
}
}
OUTPUT:
Sum: 30
MODULE 3
11) Explain class with an example.
Ans:
A class is declared by use of the class keyword. The classes that have been used up to this point
are actually very limited examples of its complete form. Classes can (and usually do) get much
more complex. A simplified general form of a class definition is :
class classname {
type instance-variable1;
type instance-variable2;
// ... type instance-variableN;
type methodname1(parameter-list)
{
// body of method }
type methodname2(parameter-list)
{
// body of method }
// ... type methodnameN(parameter-list)
{
// body of method
}
}
Example: Rectangle Class:
class Rectangle {
// Instance variables
int length;
int width;
// Method 1: Calculate area
int calculateArea() {
return length * width;
ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY
E-SECTION
JAVA Q AND A FOR IA 2
}
// Method 2: Calculate perimeter
int calculatePerimeter() {
return 2 * (length + width);
}
}
public class Main {
public static void main(String[] args) {
// Create a Rectangle object
Rectangle rect = new Rectangle();
// Set instance variables
rect.length = 5;
rect.width = 3;
// Call methods and display results
System.out.println("Area: " + rect.calculateArea());
System.out.println("Perimeter: " + rect.calculatePerimeter());
}
}
Output:
Area: 15
Perimeter: 16
12) Explain how objects are declared with an example.
Ans:
Declaring Object:
Obtaining objects of a class is a two-step process.
First, you must declare a variable of the class type. This variable does not define an object.
Instead, it is simply a variable that can refer to an object.
Second, you must acquire an actual, physical copy of the object and assign it to that variable.
You can do this using the new operator.
Syntax to Declare and Initialize an Object:
ClassName objectName = new ClassName();
ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY
E-SECTION
JAVA Q AND A FOR IA 2
Example :
// Define the Dog class
class Dog {
// Field (attribute)
String name;
// Method to display dog's name
void displayName() {
System.out.println("Dog's Name: " + name);
}
}
public class Main {
public static void main(String[] args) {
// Declare and initialize a Dog object
Dog myDog = new Dog(); // Object declaration
// Set value for the dog's name
myDog.name = "Buddy";
// Call the method to display the dog's name
myDog.displayName();
}
}
OUTPUT:
Dog's Name: Buddy
13) Explain the general form of a method with an example.
Ans :
Introducing Methods
This is the general form of a method:
type name(parameter-list)
{
// body of method
}
• Here, type specifies the type of data returned by the method. This can be any valid type,
including class types that you create. If the method does not return a value, its return type
must be void.
Following form of the return statement:
ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY
E-SECTION
JAVA Q AND A FOR IA 2
return value;
Here, value is the value returned.
In fact, methods define the interface to most classes.
This allows the class implementor to hide the specific layout of internal data structures behind
cleaner method abstractions.
In addition to defining methods that provide access to data, you can also define methods that
are used internally by the class itself.
Example:
// This program includes a method inside the box class.
class Box
{
double width;
double height;
double depth;
// display volume of a box
void volume()
{
System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}
NOTE{FOR MORE INFORMATION REFER NOTES AND PPT AND MODEL QUESTION PAPERS }
** THANK YOU **
SACHIN
DATA SCIENCE
E-SECTION