Assignment 2&3
1. Which keyword is used to create an object in Java?
a) class
b) new
c) object
d) create
2. Which of the following defines a class correctly?
a) class Car()
b) def class Car
c) class Car { }
d) Car class { }
3. What is the purpose of a constructor?
a) Destroy objects
b) Create arrays
c) Initialize objects
d) Convert types
4. Which of the following allows multiple methods with the same name in a class?
a) Method overriding
b) Method hiding
c) Method overloading
d) Method nesting
5. What is the correct syntax to access a class member?
a) object->member
b) object.member
c) object:member
d) object#member
6. Which keyword defines a static member?
a) const
b) shared
c) static
d) class
7. Which method is automatically called when an object is created?
a) main()
b) init()
c) constructor()
d) Constructor (with class name)
8. Which keyword is used to define a method that cannot be overridden?
a) const
b) final
c) static
d) private
9. What is inheritance in Java?
a) Copying one class into another
b) Creating multiple classes from one class
c) Acquiring properties of one class by another
d) Declaring methods in classes
10. Which keyword is used to inherit a class?
a) inherits
b) this
c) extends
d) superclass
11. What is method overriding?
a) Declaring multiple methods with the same name and parameters
b) Using a superclass method as-is
c) Redefining a method in a subclass
d) None of the above
12. What happens if a class is declared final?
a) It cannot be instantiated
b) It cannot be inherited
c) Its variables cannot be changed
d) It cannot have a main method
13. The finalize() method is used for:
a) Compiling code
b) Managing memory before garbage collection
c) Initializing objects
d) Protecting methods
14. Which keyword is used to prevent a method from being overridden?
a) protected
b) private
c) static
d) final
15. An abstract class:
a) Cannot contain concrete methods
b) Cannot be extended
c) Cannot be instantiated
d) Must contain only variables
16. Which of the following is a valid abstract method?
a) void draw() {}
b) abstract void draw();
c) void draw() = 0;
d) draw() abstract;
17. Which access modifier provides the least accessibility?
a) public
b) private
c) protected
d) default
18. Which access modifier allows visibility within the same package only?
a) private
b) default
c) public
d) protected
19. How is a one-dimensional array declared in Java?
a) int arr[] = new int[5];
b) int[] arr = int[5];
c) int arr() = new int[5];
d) int arr = new int[];
20. What is the index of the first element in an array?
a) -1
b) 0
c) 1
d) Depends on the compiler
21. How do you declare a 2D array?
a) int arr = new int[2];
b) int arr[][] = new int[3][3];
c) int arr[] = new int[3][3];
d) array arr[2][2];
22. Which method returns the length of a string in Java?
a) size()
b) length
c) length()
d) getLength()
23. Which package contains the Vector class?
a) java.io
b) java.util
c) java.vector
d) java.lang
24. Which of these is a wrapper class for int?
a) Int
b) IntClass
c) Integer
d) intObject
25. What is autoboxing in Java?
a) Converting an object to a primitive
b) Automatically importing classes
c) Converting a primitive to a wrapper class
d) Wrapping methods in classes
26. Which of the following can be overloaded in a class?
a) Constructors only
b) Methods only
c) Both methods and constructors
d) Neither
27. Which constructor is provided by Java if none is defined?
a) Copy constructor
b) Parameterized constructor
c) Default constructor
d) Private constructor
28. What does this keyword refer to?
a) Current method
b) Superclass
c) Current object
d) Main class
29. Which keyword is used to call a superclass constructor?
a) super()
b) this()
c) parent()
d) base()
30. Which of the following is true about static methods?
a) Can access non-static data directly
b) Can only access static data
c) Can use this keyword
d) Always abstract
31. What will happen if you try to override a final method?
a) Compilation error
b) Runtime error
c) Works normally
d) Skips method
32. Which of the following defines polymorphism in Java?
a) Abstraction
b) Overloading and Overriding
c) Inheritance only
d) Encapsulation
33. Which of these allows abstract methods?
a) Interface
b) Final class
c) Constructor
d) Static block
34. Which statement is true about an abstract class?
a) Cannot have any implemented methods
b) Must be inherited to be used
c) Can be instantiated directly
d) Can only contain variables
35. Which keyword is not used for visibility control in Java?
a) private
b) public
c) secure
d) protected
36. What is the default value of an uninitialized array of integers?
a) 0
b) null
c) undefined
d) garbage
37. Which of the following is true about String in Java?
a) Strings are mutable
b) Strings are objects
c) Strings are stored as arrays
d) Strings require special class imports
38. What does toString() method do in Java?
a) Converts int to string
b) Returns string representation of an object
c) Parses a string
d) Converts string to char array
39. Which method is used to add an element to a Vector?
a) insert()
b) append()
c) add()
d) push()
40. Which wrapper class method converts a string to integer?
a) Integer.toInt()
b) Integer.parseInt()
c) parse.Integer()
d) String.toInt()
41. Which of the following is an arithmetic operator in Java?
a) &&
b) ++
c) +
d) ==
42. What will be the result of 10 % 3 in Java?
a) 1
b) 0
c) 3
d) 10
43. Which operator is used to check if two values are equal?
a) =
b) ==
c) !=
d) &&
44. Which operator increases the value of a variable by 1?
a) --
b) ++
c) +=
d) **
45. What is the result of the expression 5 + 2 * 3?
a) 21
b) 11
c) 13
d) 7
46. What is the precedence of arithmetic operators in Java?
a) +, -, *, /
*b) , / before +, -
c) Left to right
d) Equal for all
47. What type of operator is ?: in Java?
a) Logical
b) Assignment
c) Conditional
d) Bitwise
48. Which of the following is a bitwise operator?
a) &&
b) ||
c) &
d) ==
49. The instanceof operator is an example of a:
a) Logical operator
b) Special operator
c) Bitwise operator
d) Relational operator
50. The logical operator || returns true if:
a) Both operands are false
b) One operand is true
c) Both operands are true
d) None of the above
51. Which statement is used for decision-making in Java?
a) loop
b) switch
c) if
d) class
52. What is the output of the following code:
if (true)
System.out.print("Hello");
else
System.out.print("World");
a) Hello
b) World
c) HelloWorld
d) Error
53. Which of the following is used to handle multiple conditions?
a) switch
b) if
c) if-else
d) else-if ladder
54. In a nested if-else statement, the else part belongs to:
a) The nearest if
b) The outermost if
c) All if blocks
d) None
55. Which keyword is used to compare a single variable with multiple values?
a) if
b) else
c) switch
d) compare
56. What type of loop is a while loop?
a) Entry controlled
b) Exit controlled
c) Conditional
d) Counter controlled
57. Which loop is guaranteed to execute at least once?
a) while
b) do-while
c) for
d) None
58. Which of the following is the correct for loop syntax?
a) for x in range(10)
b) for(int i = 0; i < 10; i++)
c) loop(i = 0; i < 10; i++)
d) for i in 10
59. The do-while loop evaluates the condition:
a) Before the loop starts
b) At the beginning of each iteration
c) At the end of each iteration
d) Only once
60. What will the following code output?
java
int i = 0;
while(i < 3) {
System.out.print(i);
i++;
a) 012
b) 123
c) 0123
d) Infinite loop