Java Practice Worksheet: Data Types, Type
Casting, and If-Else Statements
Part A: Data Types (Basic Understanding)
1. What are the two categories of data types in Java?
2. List all primitive data types in Java.
3. What is the default value of the following data types: a) int b) boolean c) float d) char
4. What is the size (in bytes) of int, double, and char?
5. Is String a primitive data type? Why or why not?
6. Which data type would you use to store: a) Age of a person b) Price of a product c) Single
character d) True/False value
Part B: Type Casting & Type Conversion
7. What is the difference between implicit and explicit type conversion?
8. Write an example of widening conversion in Java.
9. Write an example of narrowing conversion in Java.
10. What will be the output of this code? int x = 10; double y = x; System.out.println(y);
11. What will be the output of this code? double a = 10.9; int b = (int) a; System.out.println(b);
12. Predict the output: char c = 'A'; int n = c + 3; System.out.println(n);
13. Is it possible to convert a boolean to an int in Java? Explain.
14. What type of conversion happens in this expression? int a = 5; double b = 6.2; double c = a + b;
15. Identify the error (if any): int x = 10; byte y = x;
Part C: If-Else Statements
16. Write a Java program to check whether a number is even or odd.
17. Write a Java program to check whether a person is eligible to vote (age ≥ 18).
18. Predict the output: int num = 5; if (num > 10) System.out.println('Greater'); else
System.out.println('Smaller');
19. Write a program to find the largest of two numbers using if-else.
20. Write a program to find the largest of three numbers using nested if-else.
21. Write a program to check if a year is a leap year or not.
22. What will be the output? int a = 10, b = 20; if (a > b) System.out.println('A'); else if (a == b)
System.out.println('Equal'); else System.out.println('B');
23. What is the difference between: if (x = 5) and if (x == 5)?
24. Can we use if without else in Java? Give an example.
25. What is the purpose of nested if statements? Give a short example.
Bonus (Challenge Questions)
26. Write a Java program to check if a number is positive, negative, or zero.
27. Write a Java program that takes a character as input and checks whether it is a vowel or
consonant.
28. What will happen if you forget to put {} after an if-statement containing multiple lines of code?
29. Write a program that reads marks of a student and prints the grade: >=90: A, >=80: B, >=70: C,
>=60: D, <60: F
30. Explain the difference between if-else and switch with an example.