[go: up one dir, main page]

0% found this document useful (0 votes)
28 views7 pages

Question 2

Imj

Uploaded by

sboloram4999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views7 pages

Question 2

Imj

Uploaded by

sboloram4999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1. What is the default access modifier for a class in Java if none is specified?

 A) Private
 B) Protected
 C) Public
 D) Package-private (default)

2. Which keyword is used to create a new object in Java?

 A) new
 B) create
 C) make
 D) instantiate

3. How does a nested if-else statement work?

 A) The else block is executed first


 B) It allows multiple conditions to be checked sequentially
 C) It executes the outer if block only if the inner if block is true
 D) It runs parallel to other if-else statements

4. Which statement is true about the else block in an if-else statement?

 A) It must be accompanied by an if statement


 B) It can exist without an if statement
 C) It can only be used with if statements in loops
 D) It must be used with the if statement

5. Which loop will always execute its body at least once regardless of the condition?

 A) for loop
 B) while loop
 C) do-while loop
 D) None of the above

6. What is the main difference between while and do-while loops?

 A) while loop checks the condition before execution; do-while loop checks it after
execution
 B) do-while loop checks the condition before execution; while loop checks it after
execution
 C) while loop executes the code block once; do-while loop can execute it multiple
times
 D) There is no difference between them

7. How do you terminate a loop early in Java?

 A) Using return
 B) Using continue
 C) Using break
 D) Using stop
8. In a for loop, what happens if the increment expression is omitted?

 A) The loop will not terminate


 B) The loop will run indefinitely
 C) The loop will execute only once
 D) The code will not compile

9. Which of the following is true about constructors in Java?

 A) Constructors must have a return type


 B) Constructors can be called explicitly
 C) Constructors are used to initialize objects
 D) Constructors can have the same name as methods but different parameters

10. How does a constructor differ from a method in Java?

 A) A constructor can have a return type


 B) A method can have the same name as the class
 C) A constructor's name must be the same as the class name, and it does not have a
return type
 D) A constructor is invoked explicitly, while methods are not

11. Which keyword is used to call a constructor from another constructor in the
same class?

 A) call
 B) this
 C) super
 D) new

12. Which of the following is true about method overloading?

 A) Method overloading is based on the return type of the method


 B) Method overloading is based on the method name and the parameter list
 C) Method overloading is not allowed in Java
 D) Method overloading is based on the access modifier

13. What does the super() keyword do in a constructor?

 A) It initializes a variable
 B) It calls the parent class’s constructor
 C) It defines a new constructor
 D) It stops the execution of the constructor

14. What does the super() keyword do in a constructor?

 A) It initializes a variable
 B) It calls the parent class’s constructor
 C) It defines a new constructor
 D) It stops the execution of the constructor
15. System.out.println(7 / 2);

 A) 3.5
 B) 3
 C) 3.0
 D) 7

16. What is the purpose of the this keyword in Java?

 A) To refer to the current object


 B) To call a superclass method
 C) To return a value from a method
 D) To declare a variable

17. Why is encapsulation important in object-oriented programming?

 A) It allows for faster execution of programs.


 B) It enables the modification of the class without affecting other classes.
 C) It automatically generates documentation.
 D) It allows any class to access the internal data of an object.

18. Which of the following is a benefit of encapsulation?

 A) Increased memory usage.


 B) Faster compilation time.
 C) Improved security and control over data.
 D) Unlimited access to all class members.

Answer: C) Impved security and control over data.

19. Which of the following statements is true about encapsulation?

 A) Encapsulation is not supported in Java.


 B) Encapsulation can be achieved using only public fields.
 C) Encapsulation prevents a class from having any methods.
 D) Encapsulation helps in data hiding and abstraction.

Answer: D) Encapsulation helps in data hiding and abstraction.

20. Which keyword is used to define a method that does not return a value?

 A) int
 B) void
 C) null
 D) static

21. Which of the following statements about methods is true?

 A) Methods must always have parameters.


 B) Methods can only return primitive data types.
 C) Methods can be overloaded by changing the number or type of parameters.
 D) Methods cannot be called within other methods.

Answer: C) Methods can be overloaded by changing the number or type of parameters.


22. What is the purpose of a method's parameter list in Java?

 A) To specify the return type of the method


 B) To define the variables that can be accessed globally
 C) To accept input values when the method is called
 D) To declare local variables within the method

Answer: C) To accept input values when the method is called

23. Which of the following describes method overloading?

 A) Defining a method inside another method


 B) Using the same method name with different parameter lists
 C) Assigning multiple return types to a method
 D) Declaring a method without a return type

Answer: B) Using the same method name with different parameter lists

24. Which of the following is a library class in Java?

 A) Main
 B) Scanner
 C) Calculator
 D) MyClass

Answer: B) Scaner

25. What is the purpose of the Scanner class in Java?

 A) To create graphical user interfaces


 B) To handle network communication
 C) To read input from the user
 D) To perform mathematical calculations

Answer: C) To read input from the user

26. What does the System.out.println() method do in Java?

 A) Reads input from the user


 B) Writes data to a file
 C) Prints output to the console
 D) Compiles the Java program

Answer: C) Prints output to the console

27. for (int i = 0; i < 5; i++) {


System.out.println(i);
}

 A) 4 times
 B) 5 times
 C) 6 times
 D) 0 times
28. int i = 0;
while (i < 3) {
System.out.println(i);
i++;
}

 A) 1 2 3
 B) 0 1 2 3
 C) 0 1 2
 D) 3 2 1 0

29. Which of the following is true about the for-each loop in Java?

 A) It can only be used with arrays.


 B) It can be used to iterate over elements in a collection or array.
 C) It requires a counter variable.
 D) It is the only loop that can iterate backward.

30. What is the role of the else statement in an if-else structure?

 A) To execute code when the if condition is true


 B) To execute code when the if condition is false
 C) To define multiple conditions
 D) To repeat code execution

Answer: B) To execute code when the if condition is false

31 Which of the following is true about nested if statements?

 A) An if statement cannot contain another if statement.


 B) Nested if statements are not allowed in Java.
 C) An if statement can contain multiple if statements inside it.
 D) Nested if statements must have an else block.

32 Which of the following logical operators is used to check if both conditions are true
in an if statement?

 A) ||
 B) &&
 C) !
 D) = =

33. Which of the following statements correctly describes the use of continue in a while
loop?

 A) It terminates the loop completely.


 B) It skips the current iteration and exits the loop.
 C) It skips the rest of the current iteration and continues with the next iteration.
 D) It restarts the loop from the beginning.

Answer: C) It skips the rest of the current iteration and continues with the next iteration.
34. int count = 0; while (count < 5) { if (count == 2) { break; } System.out.println(count); count++; }

 A) 0 1
 B) 0 1 2 3 4
 C) 0 1 2

 D) 0 1 3 4

35. What is the purpose of an if statement in Java?

 A) To iterate over a block of code multiple times


 B) To define a class
 C) To execute a block of code based on a condition
 D) To terminate the program

36. What is an object in Java?

 A) A variable that holds multiple values


 B) An instance of a class
 C) A method that performs a specific task
 D) A collection of data and methods

37. Which of the following best describes encapsulation?

 A) The ability to inherit properties from another class


 B) The ability to define multiple methods with the same name
 C) The bundling of data and methods that operate on that data within one unit
 D) The division of a program into separate modules

Answer: C) The bundling of data and methods that operate on that data within one unit

38. What is abstraction in Java?

 A) Hiding the internal implementation details and showing only essential features
 B) The process of defining multiple classes with the same name
 C) The ability to define private variables
 D) The ability to create an instance of a class

Answer: A) Hiding the internal implementation details and showing only essential features

39. What is an abstract method in Java?

 A) A method with a body defined in the abstract class


 B) A method that cannot be overridden
 C) A method without a body that must be implemented by subclasses
 D) A method that is private and final

Answer: C) A method without a body that must be implemented by subclasses

40. Which of the following statements is true about abstract classes?

 A) An abstract class cannot have constructors.


 B) An abstract class can have both abstract and non-abstract methods.
 C) An abstract class must have at least one abstract method.
 D) An abstract class cannot have instance variables.
Answer: B) An abstract class can have both abstract and non-abstract methods.

You might also like