70 Java Final Exam Questions PDF

Summary

This document contains 70 questions covering various aspects of Java programming, including true/false, multiple choice, and fill-in-the-blank questions. The questions are suitable for undergraduate-level Java programming practice and final exams. The questions cover a range of topics, from basic concepts to more advanced topics.

Full Transcript

### **70 Questions for the Java Final Exam** --- ### **Section 1: True/False (15 Questions)** 1. Java is case-sensitive. (True/False) 2. A class can contain multiple constructors. (True/False) 3. The `main` method in Java must always be `public` and `static`. (True/False) 4. A `fin...

### **70 Questions for the Java Final Exam** --- ### **Section 1: True/False (15 Questions)** 1. Java is case-sensitive. (True/False) 2. A class can contain multiple constructors. (True/False) 3. The `main` method in Java must always be `public` and `static`. (True/False) 4. A `final` variable can be modified after it is initialized. (True/False) 5. The `this` keyword is used to refer to the current instance of a class. (True/False) 6. The `static` keyword allows a variable to belong to a class rather than any specific instance. (True/False) 7. A method in a subclass can override a `private` method in the parent class. (True/False) 8. An abstract class can have both abstract and non-abstract methods. (True/False) 9. A constructor can have a return type. (True/False) 10. Static methods cannot access non-static variables directly. (True/False) 11. The `super` keyword is used to call the constructor of the parent class. (True/False) 12. In Java, an array index starts at 1. (True/False) 13. `break` can be used to exit a loop or a `switch` statement. (True/False) 14. An interface in Java can contain instance variables. (True/False) 15. You can declare multiple classes in a single Java file, but only one class can be public. (True/False) --- ### **Section 2: Multiple Choice (20 Questions)** 16. What is the size of an `int` in Java? a) 1 byte b) 2 bytes c) 4 bytes d) 8 bytes 17. Which keyword is used to inherit a class? a) `extends` b) `implements` c) `super` d) `this` 18. What is the default value of a `boolean` variable? a) true b) false c) null d) 0 19. Which access modifier makes a member accessible only within its class? a) `public` b) `protected` c) `default` d) `private` 20. What type of loop ensures the code executes at least once? a) `for` b) `while` c) `do-while` d) `foreach` 21. How do you declare an array of size 10? a) `int array;` b) `int[] array = new int;` c) `int array[] = 10;` d) `array int;` 22. Which of the following is used to read input from the user? a) `Scanner` b) `BufferedReader` c) `System.in` d) All of the above 23. What is the output of `5 + "5"`? a) 10 b) 55 c) Error d) None 24. What keyword is used to stop a loop immediately? a) `continue` b) `break` c) `return` d) `exit` 25. Which method is used to find the length of an array? a) `size()` b) `length` c) `length()` d) `count()` 26. What does `super()` do? a) Refers to the current class object b) Calls the parent class constructor c) Calls a static method d) Refers to a local variable 27. What is an abstract class? a) A class with only static methods b) A class that cannot be instantiated c) A class without constructors d) A class without variables 28. What happens when a program does not include the `main` method? a) It runs successfully b) It throws a runtime error c) It throws a compilation error d) None 29. How is memory allocated for a static variable? a) At object creation b) At class loading c) At method call d) At runtime 30. Which keyword is used to prevent method overriding? a) `static` b) `private` c) `final` d) `protected` 31. Which operator is used to compare two values? a) `=` b) `==` c) `!=` d) `&&` 32. What is the purpose of the `this` keyword? a) Refers to a static variable b) Refers to the current object c) Refers to the parent class d) Refers to a local variable 33. What is the result of `10 / 3` in Java? a) 3.333 b) 3 c) 4 d) None 34. Which loop is used to iterate over elements of an array directly? a) `for` b) `while` c) `foreach` d) `do-while` 35. What is the purpose of a constructor? a) To create a new class b) To initialize object properties c) To delete objects d) To call methods --- ### **Section 3: Fill in the Blanks (15 Questions)** 36. The default value of a numeric array element is __________. 37. A `__________` class cannot be instantiated. 38. The keyword used to define a subclass is __________. 39. The `__________` keyword refers to the parent class in Java. 40. `__________` methods cannot be overridden in a child class. 41. An array index in Java starts at __________. 42. The `__________` loop checks the condition after executing the code block. 43. A `__________` variable is shared among all objects of a class. 44. The `__________` keyword is used to terminate a loop. 45. A method with the same name but a different parameter list is called __________. 46. The `__________` operator is used to compare two values. 47. A method with no implementation is called a __________ method. 48. The size of a `char` data type is __________ bytes. 49. Java programs start execution from the __________ method. 50. A variable declared inside a method is called a __________ variable. --- ### **Section 4: Output Prediction (10 Questions)** 51. ```java System.out.println(2 + "3"); ``` 52. ```java int[] arr = {1, 2, 3}; System.out.println(arr.length); ``` 53. ```java for (int i = 0; i < 3; i++) { if (i == 1) continue; System.out.print(i + " "); } ``` 54. ```java System.out.println(5 * 2 / 2 + 3); ``` 55. ```java int a = 10; int b = a++; System.out.println(b); ``` --- ### **Section 5: Coding Questions (10 Questions)** 56. Write a program to print numbers from 1 to 10 using a `for` loop. 57. Write a program to check if a number is even or odd. 58. Create a `Student` class with `name` and `age` attributes. Add a method to display the details. 59. Write a program to calculate the sum of all elements in an array. 60. Write a program to find the largest of three numbers. 61. Create a method to calculate the factorial of a number. 62. Write a program to demonstrate constructor overloading. 63. Create a `Rectangle` class with `width` and `height`, and calculate its area. 64. Write a program to print all even numbers from 1 to 20 using a `while` loop. 65. Write a program to reverse an array. --- ### **Section 6: Bonus Questions (5 Questions)** 66. What is method overriding? 67. What is the difference between `this` and `super`? 68. Explain the purpose of the `final` keyword. 69. Can we overload the `main` method in Java? 70. Why is Java not supporting multiple inheritance? --- **هذه الأسئلة شاملة ومتنوعة بين السهلة والمتوسطة والصعبة. حلّها بتركيز وستكون جاهزًا للامتحان! بالتوفيق 🌟🚀**

Use Quizgecko on...
Browser
Browser