Java Basic Programming Questions with Answers (Data Types and
Operators)
1. 1. Write a Java program to demonstrate all primitive data types.
public class DataTypesDemo {
public static void main(String[] args) {
byte b = 10;
short s = 1000;
int i = 50000;
long l = 1000000000L;
float f = 5.75f;
double d = 19.99;
char c = 'A';
boolean bool = true;
System.out.println("byte: " + b);
System.out.println("short: " + s);
System.out.println("int: " + i);
System.out.println("long: " + l);
System.out.println("float: " + f);
System.out.println("double: " + d);
System.out.println("char: " + c);
System.out.println("boolean: " + bool);
}
}
2. 2. Write a program to add, subtract, multiply and divide two numbers.
public class ArithmeticOperations {
public static void main(String[] args) {
int a = 20, b = 10;
System.out.println("Addition: " + (a + b));
System.out.println("Subtraction: " + (a - b));
System.out.println("Multiplication: " + (a * b));
System.out.println("Division: " + (a / b));
}
}
3. 3. Write a program to find the average of three numbers.
public class Average {
public static void main(String[] args) {
int a = 10, b = 20, c = 30;
double avg = (a + b + c) / 3.0;
System.out.println("Average: " + avg);
}
}
4. 4. Write a program to swap two numbers using a temporary variable.
public class SwapWithTemp {
public static void main(String[] args) {
int a = 5, b = 10, temp;
temp = a;
a = b;
b = temp;
System.out.println("a = " + a + ", b = " + b);
}
}
5. 5. Write a program to swap two numbers without using a third variable.
public class SwapWithoutTemp {
public static void main(String[] args) {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
System.out.println("a = " + a + ", b = " + b);
}
}
6. 6. Write a program to check whether a number is even or odd.
public class EvenOdd {
public static void main(String[] args) {
int num = 7;
if(num % 2 == 0)
System.out.println("Even");
else
System.out.println("Odd");
}
}
7. 7. Write a program to find the largest among three numbers.
public class Largest {
public static void main(String[] args) {
int a = 25, b = 40, c = 15;
if(a >= b && a >= c)
System.out.println("Largest: " + a);
else if(b >= c)
System.out.println("Largest: " + b);
else
System.out.println("Largest: " + c);
}
}
8. 8. Write a program to check if a character is a vowel or consonant.
public class VowelConsonant {
public static void main(String[] args) {
char ch = 'e';
if("aeiouAEIOU".indexOf(ch) != -1)
System.out.println(ch + " is a Vowel");
else
System.out.println(ch + " is a Consonant");
}
}
9. 9. Write a program to demonstrate implicit and explicit type casting.
public class TypeCasting {
public static void main(String[] args) {
int i = 100;
long l = i; // Implicit casting
double d = l;
System.out.println("Implicit casting: " + d);
double x = 55.66;
int y = (int)x; // Explicit casting
System.out.println("Explicit casting: " + y);
}
}
10. 10. Write a program to check if a number is positive, negative or zero.
public class NumberCheck {
public static void main(String[] args) {
int num = -10;
if(num > 0)
System.out.println("Positive");
else if(num < 0)
System.out.println("Negative");
else
System.out.println("Zero");
}
}