Java Fundamentals: Programs and Concepts
Data Types - Swapping using third variable
public class SwapUsingThird {
public static void main(String[] args) {
int a = 10, b = 20, temp;
temp = a;
a = b;
b = temp;
System.out.println("After Swapping: a = " + a + ", b = " + b);
}
}
Data Types - Global variable and print in main
public class GlobalVariable {
static int globalNumber = 50; // Global variable
public static void main(String[] args) {
System.out.println("Global number is: " + globalNumber);
}
}
Operators - Arithmetic sum
public class ArithmeticSum {
public static void main(String[] args) {
int a = 15, b = 25, sum;
sum = a + b;
System.out.println("Sum = " + sum);
}
}
Operators - Swapping using Arithmetic operations
public class SwapUsingArithmetic {
public static void main(String[] args) {
int a = 10, b = 20;
a = a + b; // 30
b = a - b; // 10
a = a - b; // 20
System.out.println("After Swapping: a = " + a + ", b = " + b);
}
}
Operators - MCQs
Java Fundamentals: Programs and Concepts
Q1. What is the output of System.out.println(10 % 3)?
a) 1 b) 0 c) 3 d) 10
Answer: a) 1
Q2. Which operator is used to compare two values?
a) = b) == c) != d) &&
Answer: b) ==
Q3. What is the result of 5 + 2 * 3?
a) 21 b) 11 c) 15 d) 7
Answer: b) 11