Java Lab practice questions
1. Arithmetic Operators
Program 1: Perform Arithmetic Operations
class ArithmeticDemo {
public static void main(String[] args) {
int a = 10, b = 5;
[Link]("Addition: " + (a + b));
[Link]("Subtraction: " + (a - b));
[Link]("Multiplication: " + (a * b));
[Link]("Division: " + (a / b));
}
}
Output:
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
2. Relational Operators
Program 2: Compare Two Numbers
class RelationalDemo {
public static void main(String[] args) {
int a = 10, b = 20;
[Link](a > b);
[Link](a < b);
[Link](a == b);
}
}
Output:
false
true
false
3. Logical Operators
Program 3: Logical AND, OR, NOT
class LogicalDemo {
public static void main(String[] args) {
boolean a = true, b = false;
[Link](a && b);
[Link](a || b);
[Link](!a);
}
}
Output:
false
true
false
4. Increment & Decrement
Program 4: Pre and Post Increment
class IncDec {
public static void main(String[] args) {
int a = 5;
[Link]("Pre: " + (++a));
[Link]("Post: " + (a++));
[Link]("Final: " + a);
}
}
Output:
Pre: 6
Post: 6
Final: 7
5. Conditional (Ternary) Operator
Program 5: Find Maximum
class TernaryDemo {
public static void main(String[] args) {
int a = 10, b = 20;
int max = (a > b) ? a : b;
[Link]("Max: " + max);
}
}
Output:
Max: 20
6. Bitwise Operator
Program 6: Bitwise AND, OR
class BitwiseDemo {
public static void main(String[] args) {
int a = 5, b = 3;
[Link](a & b);
[Link](a | b);
}
}
Output:
1
7
7. Dot Operator
Program 7: Access Method using Dot
class DotDemo {
public static void main(String[] args) {
[Link]("Hello".length());
}
}
Output:
5
8. Operator Precedence
Program 8: Expression Evaluation
class PrecedenceDemo {
public static void main(String[] args) {
int result = 10 + 5 * 2;
[Link](result);
}
}
Output:
20
9. If Statement
Program 9: Check Positive
class IfDemo {
public static void main(String[] args) {
int num = 10;
if (num > 0)
[Link]("Positive");
}
}
Output:
Positive
10. If-Else Statement
Program 10: Even or Odd
class IfElseDemo {
public static void main(String[] args) {
int num = 7;
if (num % 2 == 0)
[Link]("Even");
else
[Link]("Odd");
}
}
Output:
Odd
11. Nested If
Program 11: Largest of Three
class NestedIf {
public static void main(String[] args) {
int a=10,b=20,c=15;
if(a>b){
if(a>c) [Link]("A largest");
} else {
if(b>c) [Link]("B largest");
else [Link]("C largest");
}
}
}
Output:
B largest
12. If-Else Ladder
Program 12: Grade System
class LadderDemo {
public static void main(String[] args) {
int marks = 75;
if(marks>=90) [Link]("A");
else if(marks>=60) [Link]("B");
else [Link]("C");
}
}
Output:
B
13. Switch Statement
Program 13: Day Finder
class SwitchDemo {
public static void main(String[] args) {
int day = 2;
switch(day){
case 1: [Link]("Monday"); break;
case 2: [Link]("Tuesday"); break;
}
}
}
Output:
Tuesday
14. While Loop
Program 14: Print 1 to 5
class WhileDemo {
public static void main(String[] args) {
int i=1;
while(i<=5){
[Link](i);
i++;
}
}
}
Output:
1
2
3
4
5
15. Do-While Loop
Program 15
class DoWhileDemo {
public static void main(String[] args) {
int i=1;
do{
[Link](i);
i++;
}while(i<=5);
}
}
Output:
1
2
3
4
5
16. For Loop
Program 16
class ForDemo {
public static void main(String[] args) {
for(int i=1;i<=5;i++)
[Link](i);
}
}
17. For-Each Loop
Program 17
class ForEachDemo {
public static void main(String[] args) {
int arr[]={1,2,3};
for(int i:arr)
[Link](i);
}
}
18. Break Statement
Program 18
class BreakDemo {
public static void main(String[] args) {
for(int i=1;i<=5;i++){
if(i==3) break;
[Link](i);
}
}
}
Output:
1
2
19. Continue Statement
Program 19
class ContinueDemo {
public static void main(String[] args) {
for(int i=1;i<=5;i++){
if(i==3) continue;
[Link](i);
}
}
}
Output:
1
2
4
5
20. Constructors (Default)
Program 20
class DefaultConst {
DefaultConst(){
[Link]("Default Constructor");
}
public static void main(String[] args) {
new DefaultConst();
}
}
21. Parameterized Constructor
Program 21
class ParaConst {
ParaConst(int a){
[Link](a);
}
public static void main(String[] args) {
new ParaConst(10);
}
}
22. this Keyword
Program 22
class ThisDemo {
int a;
ThisDemo(int a){
this.a=a;
}
void show(){
[Link](a);
}
public static void main(String[] args){
new ThisDemo(5).show();
}
}
23. Arrays
Program 23
class ArrayDemo {
public static void main(String[] args) {
int arr[]={1,2,3};
for(int i:arr)
[Link](i);
}
}
24. String and StringBuffer
Program 24
class StringDemo {
public static void main(String[] args) {
String s="Hello";
StringBuffer sb=new StringBuffer("World");
[Link](s+" "+sb);
}
}
25. Inheritance + Method Overriding
Program 25
class Parent {
void show(){
[Link]("Parent");
}
}
class Child extends Parent{
void show(){
[Link]("Child");
}
public static void main(String[] args){
Child c=new Child();
[Link]();
}
}
Output:
Child
26. Command Line Arguments
Program 26: Sum using Command Line Arguments
class CommandLineDemo {
public static void main(String[] args) {
int a = [Link](args[0]);
int b = [Link](args[1]);
[Link]("Sum: " + (a + b));
}
}
Output (example):
java CommandLineDemo 5 10
Sum: 15
27. Varargs (Variable-Length Arguments)
Program 27: Sum using Varargs
class VarargsDemo {
static void sum(int... numbers) {
int s = 0;
for(int n : numbers)
s += n;
[Link]("Sum: " + s);
}
public static void main(String[] args) {
sum(1,2,3,4);
}
}
Output:
Sum: 10
28. Visibility Control (Public, Private)
Program 28: Access Modifiers
class AccessDemo {
private int a = 10;
public void show() {
[Link](a);
}
public static void main(String[] args) {
AccessDemo obj = new AccessDemo();
[Link]();
}
}
Output:
10
29. Interface Implementation
Program 29: Interface Example
interface Test {
void display();
}
class InterfaceDemo implements Test {
public void display() {
[Link]("Interface Method");
}
public static void main(String[] args) {
InterfaceDemo obj = new InterfaceDemo();
[Link]();
}
}
Output:
Interface Method
30. Method Overloading
Program 30: Method Overloading Example
class OverloadDemo {
void add(int a, int b) {
[Link](a + b);
}
void add(int a, int b, int c) {
[Link](a + b + c);
}
public static void main(String[] args) {
OverloadDemo obj = new OverloadDemo();
[Link](2,3);
[Link](1,2,3);
}
}
Output:
5
6