IT5L Midterm Reviewer PDF

Document Details

Uploaded by Deleted User

UM Digos College

Tags

java programming oop concepts programming principles computer science

Summary

This document contains a midterm exam for an IT course, specifically focusing on Java programming and object-oriented programming (OOP) concepts. The questions cover various aspects of Java, including access modifiers, inheritance, polymorphism, encapsulation, and static members.

Full Transcript

**Name: \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_** **Permit no. \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_** **Direction:** Each item is followed by several possible answers. Read the questions carefully and choose the letter that wil...

**Name: \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_** **Permit no. \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_** **Direction:** Each item is followed by several possible answers. Read the questions carefully and choose the letter that will corresponds to the answer that specifies pertains to you. 1. **What is the purpose of the default access modifier (package-private) in Java?** a. **To make the member accessible only within the class.** b. **To allow access to the member from any class, even in different packages.** c. **To restrict access to the member only to classes in the same package.** d. **To allow access only within the current package and its subclasses.** 2. **Which of the following statements is true about Java inheritance?** a. **A subclass can inherit from multiple classes.** b. **A subclass can inherit both instance and static variables from a superclass.** c. **A subclass cannot inherit methods from the superclass.** d. **A subclass cannot be a subclass of another subclass.** 3. **Which type of inheritance does Java support?** a. **Single inheritance only** b. **Multiple inheritance (class to class)** c. **Multilevel inheritance** d. **Java supports both single and multiple inheritance.** 4. **What is the primary purpose of the constructor in Java?** a. **To define the class name** b. **To initialize objects of a class** c. **To handle exceptions** d. **To print output on the screen** 5. **What is the effect of the static modifier when applied to a method in Java?** a. **The method can be called only from an instance of the class.** b. **The method can be called without creating an instance of the class.** c. **The method cannot access instance variables.** d. **The method can only be used by other static methods** 6. **In Java, which access modifier is commonly used to declare fields in encapsulation?** a. **Public c. Protected** b. **Private d. default (package-private)** 7. **What is inheritance in Java?** a. **A mechanism to allow objects to interact with each other.** b. **A mechanism to restrict access to methods and variables of a class.** c. **A mechanism to acquire the properties and behaviors of another class.** d. **A mechanism to make a class abstract.** 8. **Which of the following is true about encapsulation in Java?** a. **It involves hiding the internal state of an object and providing access through public methods.** b. **It allows direct access to an object\'s fields.** c. **It is the same as inheritance.** d. **It prevents creating setter methods for private fields** 9. **What is the effect of declaring a method as static in terms of memory usage in Java?** a. **It increases memory usage.** b. **It decreases memory usage.** c. **It has no effect on memory usage.** d. **It depends on the size of the class.** 10. **How is a static method called in Java?** a. **object.method()** b. **ClassName.method()** c. **method.ClassName()** d. **new method()** 11. **Which of the following statements is true regarding the private access modifier?** a. **A private member can be accessed by any other class in the same package.** b. **A private member can be accessed only within the same class.** c. **A private member can be accessed by subclasses.** d. **A private member can be accessed by any class in any package.** 12. **Can a static method access non-static (instance) variables of the class?** a. **Yes** b. **No** c. **Only if the variables are declared as final.** d. **It depends on the method implementation** 13. **What will happen if we try to access a private field directly outside of its class in Java?** a. **It will cause a compile-time error.** b. **The value of the field will be returned, but no changes can be made.** a. **It will work as expected because private fields are accessible anywhere.** b. **The compiler will automatically make the field public for external access.** 14. **Which of the following can be inherited in Java?** a. **Private methods c. Final methods** b. **Static variables d. All of the above** 15. **What does the \@Override annotation do in Java?** a. **It ensures that a method is overridden in the subclass.** b. **It creates a new method in the superclass.** c. **It indicates that a method cannot be overridden.** d. **It provides default behavior for the method** 16. **What does the final modifier do when applied to a class?** a. **The class cannot be subclassed.** b. **The class cannot have any methods.** c. **The class cannot contain variables.** d. **The class cannot be used as an interface** 17. **What is the significance of the \'this\' keyword in a static method?** a. **It refers to the current instance of the class.** b. **It is used to call another static method.** c. **It is not allowed in static methods.** d. **It refers to the superclass instance** 18. **Which access modifier allows members to be accessible from anywhere in the program?** a. **Private c. Public** b. **Protected d. Default** 19. **Which of the following is an example of compile-time polymorphism in Java?** a. **Method overriding** b. **Method overloading** c. **Dynamic method dispatch** d. **Constructor overloading** 20. **Which keyword is used to create an object of a class in Java?** a. **New c. Create** b. **This d. Object** 21. **Which of the following is true about the execution of static methods in Java?** a. **They are executed at runtime.** b. **They are called using an instance of the class.** c. **They are loaded and executed when the class is loaded.** d. **They cannot be called from other classes** 22. **Which of the following is an example of encapsulation in Java?** a. **Making all fields of a class public** b. **Declaring a class and allowing direct access to all its fields** c. **Creating a class with private fields and public getter/setter methods to access them** d. **Using inheritance to modify fields directly** 23. **What is polymorphism in Java?** a. **The ability of an object to take on multiple forms** b. **The ability of a class to inherit from multiple classes** c. **The ability to access private methods from outside the class** d. **The ability of a method to return multiple values** 24. **What happens if you try to access a non-static method from within a static method without creating an instance of the class?** a. **It results in a compilation error.** b. **It leads to a runtime exception.** c. **It is allowed.** d. **It depends on the access modifier of the method** 25. **What happens to the original value of a primitive variable when it is passed to a method in Java?** a. **It is directly modified.** b. **It remains unchanged.** c. **It is passed by reference.** d. **It becomes null** 26. **What is the purpose of a setter method in Java?** a. **To retrieve the value of a private field** b. **To modify the value of a private field** c. **To protect the field from being accessed** d. **To change the type of a field** 27. **Which of the following statements is true about a static variable in Java?** a. **A static variable can have different values for each instance of the class.** b. **A static variable is shared by all instances of the class and belongs to the class itself.** c. **A static variable cannot be initialized in the constructor.** d. **A static variable can only be used in static methods** 28. **What is method overloading in Java?** a. **When two methods in the same class have the same name and the same number of parameters.** b. **When two methods in the same class have the same name and different return types.** c. **When two methods in the same class have the same name and different parameter lists.** d. **When a method in a subclass overrides a method in the superclass.** 29. **Which keyword is used to define a static method in Java?** a. **static c. void** b. **method d. staticmethod** 30. **In which situations would you use the protected access modifier?** a. **When you want a member to be accessible only within the same class.** b. **When you want a member to be accessible within the same package and to any subclass.** c. **When you want a member to be accessible by any class in any package.** d. **When you want a member to be hidden from all other classes.** 31. **In Java, which keyword is used to establish inheritance between classes?** a. **Extends c. This** b. **Super d. Implements** 32. **Which of the following is an example of runtime polymorphism in Java?** a. **Method overloading** b. **Constructor overloading** c. **Method overriding** d. **Multiple inheritance** 33. **In Java, can a static method be overridden in a subclass?** a. **Yes** b. **No** c. **It depends on the access modifier of the method.** d. **Only if the subclass is in the same package** 34. **In Java, how are object references passed to methods?** a. **Pass by reference c. Pass by pointer** b. **Pass by value d. Pass by address** 35. **What is the purpose of declaring a method as static in Java?** a. **It allows the method to be accessed without creating an instance of the class.** b. **It makes the method non-modifiable.** c. **It specifies that the method returns a constant value.** d. **It restricts the method to be accessed only within the same package** 36. **What is the main purpose of encapsulation in Java?** a. **To allow multiple objects to share the same data** b. **To make a class's data private and restrict direct access** c. **To allow direct access to class variables** d. **To improve performance by reducing memory usage** 37. **In Java, what is true about the Object class in the context of inheritance?** a. **The Object class is the superclass of all classes, and every class in Java implicitly inherits from it.** b. **The Object class is only the superclass of String and Integer.** c. **The Object class cannot be inherited by other classes.** d. **Only the String class inherits from Object.** 38. **What is the outcome when modifying the content of an object inside a method in Java?** a. **It affects the original object outside the method.** b. **It has no effect on the original object.** c. **It depends on the type of the object.** d. **It leads to a compilation error** 39. **Which of the following is a valid way to call a static method from another class in Java?** a. **ClassName.method(); c. this.method();** b. **method(); d. call method();** 40. **Why is encapsulation considered an important principle in Java OOP?** c. **It helps in modifying code without affecting other parts of the program.** d. **It prevents the use of constructors in a class.** e. **It allows direct access to private fields.** c. **It prevents inheritance** 41. **What is the primary benefit of polymorphism in Java?** a. **It allows methods to have different names in different classes.** b. **It helps in method overloading.** c. **It allows a method to behave differently based on the object type.** d. **It forces a class to implement all methods** 42. **Which of the following is an example of a \"getter\" method in Java?** a. **public void setAge(int age) { this.age = age; }** b. **public int getAge() { return age; }** c. **private int age;** d. **public void age() { }** 43. **What is the scope of a protected member in Java?** a. **It can be accessed only within the same class.** b. **It can be accessed only within the same package.** c. **It can be accessed in the same package and by subclasses.** d. **It can be accessed by any class in any package** 44. **What happens if a final method is declared in a class?** a. **The method cannot be overridden by any subclass.** b. **The method can only be called by other methods in the same class.** c. **The method must be static.** d. **The method cannot have any parameters** 45. **In Java, can a static method be synchronized?** a. **Yes** b. **No** c. **Only if it has a return type.** d. **Only if it is declared as private** 46. **What are the key components of encapsulation in Java?** a. **Class, methods, and variables** b. **Fields, constructors, and method accessors** c. **Private fields, getters, and setters** d. **Inheritance and polymorphism** 47. **Which of the following is the most restrictive access modifier in Java?** a. **Public c. private** b. **Protected d. default** 48. **Which of the following is used to define a class in Java?** a. **Class c. Public** b. **Object d. Method** 49. **In Java, is it possible to call a static method using an instance of the class?** a. **Yes** b. **No** c. **Only if the static method has a return value.** d. **Only if the static method is declared as public.** 50. **If no access modifier is specified for a class member, what is its default access level in Java?** a. **Private** b. **Public** c. **Protected** d. **Default (package-private)** 51. **Which of the following is not a feature of Object-Oriented Programming in Java?** c. **Encapsulation** d. **Inheritance** e. **Compilation** f. **Polymorphism** 52. **What does encapsulation in Java primarily refer to?** g. **Hiding the implementation details and showing only the functionality** h. **The ability to inherit properties from a parent class** i. **Creating multiple methods with the same name** j. **Making classes and objects accessible to other classes** 53. **Which access modifier is used to implement encapsulation by restricting access to class members?** k. **Public** l. **Private** m. **Protected** n. **Default** 54. **Which of the following keywords is used to inherit a class in Java?** o. **This** p. **Super** q. **Extends** r. **implements** 55. **Which of the following is true about method overriding in Java?** s. **The method in the child class must have a different name than in the parent class** t. **Method overriding can be done only with static methods** u. **The method in the child class must have the same signature as the one in the parent class** v. **The method in the child class must have a different return type than the one in the parent class** 56. **What is the main advantage of polymorphism in Java?** w. **It allows methods to have the same name but different parameters** x. **It allows objects to be created from abstract classes** y. **It allows methods in subclasses to override methods in the superclass** z. **It allows classes to inherit features from multiple classes** 57. **Which of the following is a characteristic of a superclass?** a. **It can only have static methods** b. **It provides common properties and behaviors for its subclasses** c. **It cannot be inherited by other classes** d. **It must implement interfaces** 58. **Which of the following is an example of polymorphism in Java?** e. **A subclass inherits from a superclass** f. **A method with the same name but different parameters in the same class** g. **An object of a class can call methods of the superclass** h. **A class implements multiple interfaces** 59. **Which keyword is used to refer to the immediate parent class in Java?** i. **This c. Parent** j. **Super d. Base** 60. **Which of the following is the correct way to define a private variable in a class?** k. **private int number;** l. **int private number;** m. **private number int;** n. **number private int;** 61. **In Java, which of the following is true about constructors?** o. **Constructors must have the same name as the class** p. **A constructor can return a value** q. **A class can have more than one constructor with different signatures** r. **Constructors are called explicitly by the programmer** 62. **Which type of inheritance is supported by Java?** s. **Multiple inheritance through classes** t. **Single inheritance through classes** u. **Multiple inheritance through interfaces** v. **Java does not support inheritance** 63. **What is the primary benefit of using inheritance in Java?** w. **Reuse of code from the parent class** x. **Better performance** y. **More flexibility in class design** z. **Ability to create abstract methods** 64. **Which of the following is the correct way to make a class immutable in Java?** a. **Use final keyword for class** b. **Make all the instance variables private and provide getter methods** c. **Make the constructor private** d. **Both A and B** 65. **Which of the following statements about constructor overloading in Java is true?** e. **Constructors can only be overloaded based on their return type** f. **Constructor overloading is not allowed in Java** g. **Constructor overloading is based on the number or type of parameters** h. **Constructors are automatically overloaded** 66. **Which of the following correctly describes a class constructor in Java?** i. **It is invoked explicitly by the programmer when creating an object** j. **It is called automatically when an object is created** k. **It can return a value** l. **It can only be used to initialize variables** 67. **Which of the following statements about constructors in Java is true?** m. **A constructor must always have a return type** n. **A constructor can be private** o. **A constructor can have the same name as any method in the class** p. **A constructor cannot accept parameters** 68. **Which of the following allows multiple classes to be treated as a single type in Java?** q. **Abstraction** r. **Polymorphism** s. **Inheritance** t. **Encapsulation** 69. **Which of the following can be declared as final in Java?** u. **A class** v. **A method** w. **A variable** x. **All of the above** 70. **Which of the following is true about method overriding in Java?** y. **The return type of the method in the child class must be different from that of the parent class** z. **The method in the child class must have the same signature as the parent class method** a. **Overriding is possible only with static methods** b. **The method in the child class can have any access modifier** 71. **Which of the following Java keywords is used to prevent the modification of a method, class, or variable?** c. **volatile** d. **private** e. **final** f. **static** 72. **What is true about method overloading in Java?** g. **Method overloading occurs when two methods have the same name but different signatures** h. **Method overloading occurs when methods in different classes have the same signature** i. **Method overloading is a form of inheritance** j. **Method overloading can only be done in abstract classes** 73. **What is true about interfaces in Java?** k. **An interface can extend a class** l. **An interface can have concrete methods** m. **A class can implement multiple interfaces** n. **An interface cannot contain constants** 74. **Which of the following is true about method overriding in Java?** o. **The overridden method must have a different return type** p. **The overridden method must have the same name, parameter list, and return type** q. **The method in the subclass can have a more restrictive access modifier** r. **The superclass method must be private** 75. **In Java, which of the following features of a class can be private?** s. **Methods** t. **Variables** u. **Inner classes** v. **All of the above** 76. **Which of the following is the correct way to instantiate an anonymous class in Java?** w. **ClassName obj = new ClassName();** x. **ClassName obj = new ClassName() { }** y. **ClassName obj = new ClassName(); obj.method();** z. **ClassName obj = new ClassName{};** 77. **Which of the following concepts describes the ability of a subclass to provide a specific implementation of a method defined in a superclass?** a. **Method overloading** b. **Method overriding** c. **Constructor overloading** d. **Constructor overriding** 78. **Which of the following is NOT true about the static keyword in Java?** e. **It can be used for variables** f. **It can be used for methods** g. **It can be used to create an instance of a class** h. **It can be used with nested classes** 79. **Which of the following statements about super() in Java is true?** i. **super() can be used to call a method of the superclass** j. **super() must be the first statement in the constructor of the subclass** k. **super() can only be used to access private fields of the superclass** l. **super() is used to invoke the parent class's static methods** 80. **Which of the following is true about the default access modifier in Java?** m. **It allows access from all other classes in any package** n. **It allows access from only the classes within the same package** o. **It allows access from subclasses in other packages** p. **It prevents access from outside the class**

Use Quizgecko on...
Browser
Browser