Written By: Imran Kashif, HOD IT Dept. Govt.
Sadiq Graduate College of Commerce, Bahawalpur
1. What is the size of int in Java?
A) 2 bytes
B) 4 bytes
C) 8 bytes
D) Depends on the system
2. Which method is the entry point of a Java program?
A) start()
B) main()
C) run()
D) init()
3. What is the default value of a boolean variable in Java?
A) true
B) false
C) 0
D) null
4. Which of the following is not a primitive data type in Java?
A) int
B) float
C) String
D) char
5. What will be the output of System.out.println(10 + 20 + "30");?
A) 3030
B) 102030
C) 3020
D) 3030
6. What is the result of 5 / 2 in Java?
A) 2.5
B) 3
C) 2
D) Compilation error
7. Which of the following is a valid data type in Java?
A) integer
B) real
Pagr # 1
Written By: Imran Kashif, HOD IT Dept. Govt. Sadiq Graduate College of Commerce, Bahawalpur
C) float
D) decimal
8. Which method is used to print in Java?
A) print.out()
B) System.out.print()
C) echo()
D) cout<<
9. Which keyword is used to define a constant variable?
A) const
B) fixed
C) final
D) static
10. Which access modifier allows access from any class?
A) private
B) public
C) protected
D) default
11. What will System.out.println(3 + 4 + "Test"); print?
A) 34Test
B) 3 + 4Test
C) 7Test
D) Test7
12. What is the size of a byte in Java?
A) 4 bits
B) 8 bits
C) 16 bits
D) 32 bits
13. How do you write a comment in Java?
A) # comment
B) // comment
C) <!-- comment -->
D) \ comment
14. What is the output of System.out.println("A" + 10);?
A) A10
B) 10A
C) Error
D) A
Pagr # 2
Written By: Imran Kashif, HOD IT Dept. Govt. Sadiq Graduate College of Commerce, Bahawalpur
15. What is the output of System.out.println(2 + 3 * 4);?
A) 14
B) 20
C) 24
D) 9
16. Which keyword is used to declare a variable in Java?
A) define
B) let
C) var
D) None of the above
17. What is the correct way to declare an integer variable in Java?
A) int x = 10;
B) integer x = 10;
C) num x = 10;
D) x = 10;
18. What is the correct way to declare a constant variable in Java?
A) const int x = 5;
B) final int x = 5;
C) static int x = 5;
D) fixed int x = 5;
19. Which of the following is a valid variable name?
A) 1value
B) _value
C) value#
D) @value
20. What will be the result of: int a = 5; int b = a + 2;?
A) a = 2, b = 5
B) a = 5, b = 7
C) a = 7, b = 5
D) Compile error
21. Which of the following can NOT be used in a variable name?
A) letters
B) numbers
C) underscore
D) space
22. Which is the correct way to declare multiple variables in Java?
A) int a = 1, b = 2;
B) int a = 1; int b = 2;
Pagr # 3
Written By: Imran Kashif, HOD IT Dept. Govt. Sadiq Graduate College of Commerce, Bahawalpur
C) Both A and B
D) None
23. Which data type would you use to store a single character in Java?
A) String
B) char
C) character
D) text
24. Which of the following is a valid declaration of a String variable?
A) String name = 'College';
B) string name = "College";
C) String name = "College";
D) char name = "College";
25. Which of these is used to assign a value to a variable?
A) ==
B) :=
C) =
D) equals
26. What is the correct way to declare a double variable?
A) double x = 10.5;
B) Double x = "10.5";
C) double x = "10.5";
D) int x = 10.5;
27. What is a valid identifier in Java?
A) class
B) 123abc
C) my_var
D) int
28. What is typecasting in Java?
A) Changing variable name
B) Changing variable type
C) Copying one variable to another
D) None of the above
29. What is widening typecasting?
A) Converting smaller type to larger type
B) Converting larger type to smaller type
C) Converting class to interface
D) Converting boolean to int
Pagr # 4
Written By: Imran Kashif, HOD IT Dept. Govt. Sadiq Graduate College of Commerce, Bahawalpur
30. What is narrowing typecasting?
A) Converting class to interface
B) Converting larger type to smaller type
C) Converting String to int
D) Converting variable to method
31. Which of the following is a widening conversion?
A) int to byte
B) double to int
C) int to long
D) float to short
32. Which type of typecasting is done automatically in Java?
A) Narrowing
B) Widening
C) String casting
D) Object casting
33. What is the correct syntax for narrowing typecasting?
A) int x = (int) 5.5;
B) int x = 5.5;
C) x = int(5.5);
D) cast int = 5.5;
34. What will be the output of the following code?
int x = (int) 7.9;
System.out.println(x);
A) 8
B) 7
C) 7.9
D) Error
35. Which of the following is a narrowing conversion?
A) byte to int
B) int to double
C) double to int
D) int to long
36. What is the result of this code?
double d = 10.99;
int x = (int) d;
System.out.println(x);
Pagr # 5
Written By: Imran Kashif, HOD IT Dept. Govt. Sadiq Graduate College of Commerce, Bahawalpur
A) 11
B) 10
C) 10.99
D) Error
37. What is the difference between print() and println() in Java?
A) No difference
B) println() adds a newline at the end
C) print() is used for errors only
D) println() is faster
38. How do you display a message along with a variable?
A) System.out.println("Value: x");
B) System.out.println("Value: " + x);
C) System.out.println("Value: ", x);
D) System.out.print("Value:" x);
39. What is the output of this code?
int a = 5;
System.out.print("a = ");
System.out.println(a);
A) a = 5
B) a =5
C) 5 = a
D) Error
40. What will be the output of this code?
int x = 5;
int y = 3;
System.out.println("Sum is: " + (x + y));
A) Sum is: x + y
B) Sum is: 53
C) Sum is: 8
D) Error
41. What is the output of the following code?
float x = 5.5f;
System.out.println(x);
A) 5
B) 5.5
C) 5.5f
D) Error
Pagr # 6
Written By: Imran Kashif, HOD IT Dept. Govt. Sadiq Graduate College of Commerce, Bahawalpur
42. Which method is best to display floating-point numbers with specific decimal places?
A) System.out.println()
B) System.out.print()
C) System.out.printf()
D) System.show()
43. What is the output of the following code?
double x = 3.14159;
System.out.printf("%.2f", x);
A) 3.14
B) 3.14159
C) 3.141
D) Error
44. What is the output of this code?
double a = 1.5, b = 2.5;
System.out.println("Sum = " + (a + b));
A) Sum = 3
B) Sum = 3.0
C) Sum = 4.0
D) Sum = 3.5
45. What is the error in the following code?
public class Test {
public static void main(String args) {
System.out.println("Hello World");
}
}
A. No error
B. main method signature is incorrect
C. System.out should be system.out
D. Missing class name
46. What is the error in this Java code?
public class Test {
public static void main(String[] args) {
int number = "100";
System.out.println(number);
}
}
Pagr # 7
Written By: Imran Kashif, HOD IT Dept. Govt. Sadiq Graduate College of Commerce, Bahawalpur
A. No error
B. Cannot convert from String to int
C. Incorrect print statement
D. Variable name is reserved
47. Which of the following code snippets has an invalid variable declaration?
A. int total = 100;
B. double _value = 45.6;
C. String class = "Java";
D. char grade = 'A';
48. What is wrong with this variable declaration?
public class Demo {
public static void main(String[] args) {
float pi = 3.14;
System.out.println(pi);
}
}
A. No error
B. float cannot hold decimal values
C. Literal 3.14 is treated as double, must append f
D. Variable pi must be final
49. Which variable declaration will cause a compile-time error?
A. long num = 100L;
B. boolean isValid = true;
C. byte b = 128;
D. char letter = 'Z';
50. Find the error in this code:
public class Main {
public static void main(String[] args) {
double x = 9.7;
int y = x;
System.out.println(y);
}
}
A. No error
B. Implicit casting from double to int not allowed
C. double must be final
D. int must be declared before double
Pagr # 8
Written By: Imran Kashif, HOD IT Dept. Govt. Sadiq Graduate College of Commerce, Bahawalpur
Pagr # 9