0% found this document useful (0 votes)
15 views

Programming

this a summary of Java OOP

Uploaded by

John Adel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Programming

this a summary of Java OOP

Uploaded by

John Adel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Programming

First: definitions

Algorithm: is a set of well-defined steps for performing a task or solving a problem.

Method: is a collection of statements that performs a specific task.

Code reuse: if a specific task is performed in several places in a program, a method can be
written once to perform that task and be executed any time it is needed.

Void method: is one that simply performs a task and then terminates.

Value-returning method: not only performs a task but also sends a value back to the code
that called it.

Method definition: consists of two general parts:

1- A header which contains the access modifier, return type, method’s name and
the parentheses

2- A body

Method header: appears at the beginning of a method definition, lists several important
things about the method, including the method’s name.

Method body: is a collection of statements that are performed when the method is
executed.

Arguments: they are values that are sent into a method.

Parameter variable: sometimes simply referred to as a parameter, is a special variable


that holds a value being passed into a method.

variable's scope: is the part of the program where the variable may be accessed by its
name.

Parameter list: the parameter variables that are declared inside the parentheses in the
method header.
Arguments are passed by value: which means that only a copy of an argument’s value is
passed into a parameter variable.

Immutable: which means that they cannot be changed.

Local variables: are variables that are declared inside a method. They are called local
because they are local to the method in which they are declared. Statements outside a
method cannot access that method’s local variables.

Local variable’s lifetime: a method’s local variables exist only while the method is
executing.

Object: it is a software component that exists in memory and serves a specific purpose in
a program.

Class: is code that describes a particular type of object. It specifies the data that an object
can hold (the object’s fields), and the actions that an object can perform (the object’s
methods). You can think of a class as a code “blueprint” that can be used to create a
particular type of object.

Instance of the class: is each object that is created from a class.

Reference variable: it doesn’t hold an actual piece of data that your program will work
with. Instead, it holds the object’s memory address.

UML diagram: stands for Unified Modeling Language. It provides a set of standard
diagrams for graphically depicting object-oriented systems.

Class's members: the fields and methods that belong to a class.

Instance method: it is a method that is designed to work on an instance of a class, and


you don’t write the word static in the header.

Accessor method: is a method that gets a value from a class’s field but doesn’t change it.

Mutator method: is a method that stores a value in a field or changes the value of a field in
some other way.

Constructor method: is a method that is automatically called when an object is created.

No-arg constructor: a constructor that doesn’t accept arguments.


(By the way, a default constructor is a no-arg constructor 😉)

Method overloading: when a method is overloaded, it means that multiple methods in the
same class have the same name but use different types of parameters.
Binding: the process of matching a method call with the correct method.

Method's signature: consists of the method’s name and the data types of the method’s
parameters, in the order that they appear.

Shadowing: it is when you create a local or a parameter variable with the same name as a
field. (we say: the local or parameter variable shadows the field).

Java API: standard library of prewritten classes.

Package: is simply a group of related classes.

Static members: members that don’t belong to any instance of a class.

Copy constructor: is simply a constructor that accepts an object of the same class as an
argument.

`this` reference variable: is a keyword which is the name of a reference variable that an
object can use to refer to itself. It is available to all non-static methods.

Inheritance: allows a new class to extend an existing class. The new class inherits the
members of the class it extends.

Superclass: it is the general class.

Subclass: it is the specialized class.

Super keyword: refers to an object’s superclass and can be used to access members of
the superclass.

Method overriding: replacing inadequate superclass methods with more suitable ones in
the subclass with same signature.

protected members: they can be accessed by methods in a subclass, and by methods in


the same package as the class.

object class: the java API has a class named object which all other classes directly or
indirectly inherit form.

polymorphism: a superclass reference variable can reference objects of a subclass.

Interface: is like a class that contains only abstract methods. An interface cannot be
instantiated.
Second: notes and usage
Methods are commonly used to break a problem into small, manageable pieces. Also, it is
used to simplify programs.

When a method is called, the JVM branches and executes the statements in its body.

Methods can also be called in a hierarchical, or layered fashion. In other words, method A
can call method B, which can then call method C. When method C finishes, the JVM
returns to method B. When method B finishes, the JVM returns to method A.

Java will not automatically convert an argument to a lower-ranking data type. This means
that a long, float, or double value cannot be passed to a method that has an int parameter
variable.

A variable is visible only to statements inside the variable’s scope. A parameter variable
scope is the method in which the parameter is declared. No statement outside the method
can access the parameter variable by its name.

When an object such as a string, is passed as an argument, it is actually a reference to the


object that is passed.

String objects in Java are immutable.


String objects cannot be changed. Anytime you use the = operator to assign a string literal
to string reference variable, a new String object is created in memory.

Different methods can have local variables with the same names because the methods
cannot see each other's local variables.

When the method begins, Local variables and its parameter variables are created in
memory and when the method ends, the local variables and parameter variables are
destroyed. This means that any value stored in a local variable is lost between calls to the
method in which the variable is declared.

The scope of a parameter variable is the entire method in which it is declared.

You must have a return statement in a value-returning method. it causes the method to
end the execution and it returns a value to the statement that called the method.

In software, an object has two general capabilities:

• An object can store data. The data stored in an object are commonly called fields.
• An object can perform operations. The operation that an object can perform are
called methods.

Primitive variables such as ints, doubles, and so forth are simply storage locations in the
computer’s memory.

The reference variable doesn't hold an actual piece of data that your program will
work with. Instead, it holds the object's memory address.
After the word new, the name of a class followed by a set of parentheses appears. This
specifies the class that the object should be created from.

Constructors normally perform initialization or setup operations, such as storing initial


values in instance fields. They are called constructors because they help construct an
object.

A local Reference variable must reference an object before it can be used. Otherwise a
compiler error will occur.

There is one class, however, that can be instantiated without the new operator: the string
class.

When an object is passed as an argument to a method, the object’s address is passed into
the method's parameter variable. As a result, the parameter references the object.

When an overloaded method is being called Java uses the method's name and parameter
list to determine which method to bind the call to.

Recall that Java provides a default constructor only when you don't write any constructor
for a class. If a class has a constructor that accepts arguments, but it doesn't have a no-
arg constructor, You cannot create an instance of the class without passing arguments to
the constructor. Therefore, anytime you write the constructor for class, and that
constructor accepts arguments, you should also write a no-arg constructor if you want to
be able to create instances of the class without passing arguments to the constructor.

Instance fields are visible to all of the class’s instance methods.

The location of a variable’s declaration determines the variable’s scope.


An import statement tells the compiler which package a class is located in.

A static class member belongs to the class, not objects instantiated from the class.

When I field is declared with the keyword static, there will be only one copy of the field in
memory, regardless of the number of instances of the class that might exist. A single copy
of a class’s static field is shared by all instances of the class.

Static methods are most often used to create utility classes that perform operations on
data, but have no need to collect and store data.

The only limitation that static methods have is that they cannot refer to non-static
members of the class. This means that any method called from a static method must also
be static. It also means that if the method uses any of the class’s fields, they must be static
as well.

When writing a method that receives the value of a reference variable as an argument, you
must take care not to accidentally modify the contents of the object that is referenced by
the variable.

When you use the == operator with reference variables, the operator compares the
memory address that the variables contain, not the contents of the objects
referenced by the variables.

You already know That a constructor automatically called when an object is created.
You also know that you cannot call a constructor explicitly, as you do other methods.
However, there is one exception in this rule: you can use the this key word to call one
constructor from another constructor in the same class.
In an inheritance relationship, the superclass constructor always executes before the
subclass constructor.

Here are three guidelines you should remember about calling a superclass constructor:

• The super statement that calls the superclass constructor may be written only in
the subclass’s constructor. You cannot call the superclass constructor from any
other method.
• The super statement that calls the superclass constructor must be the first
statement in the subclass’s constructor. This is because the superclass’s
constructor must executed before the code in the subclass’s constructor executes.
• If a subclass constructor does not explicitly call a superclass constructor, Java will
automatically call the superclass’s default constructor, or no-arg constructor, just
before the code in the subclass’s constructor executes.

In order for overriding to occur, the subclass method must have the same signature as the
superclass method.

The following list summarizes the distinction between overloading and overriding:

• If two methods have the same name but different signatures, they are
overloaded. This is true where the methods are in the same class or where one
method is in the superclass and the other method is the subclass.
• If a method in a subclass has the same signature as a method in the
superclass, the subclass method overrides the superclass method.

When a subclass overloads a superclass method, both methods may be called with a
subclass object. However, when a subclass overrides a superclass method, only the
subclass’s version of the method can be called with a subclass object.

When a method is declared with the final modifier, it cannot be overridden in a subclass.

A protected member is not quite private, because it may be accessed by some methods
outside the class. Protected members are not quite public either because access to them
is restricted to methods in the same class, subclasses, and classes in the same package
as the member’s class. A protected member’s access is somewhere between private and
public.

There is a subtle difference between protected access and package access. Protected
members may be accessed by methods in the same package or in a subclass. This is true
even if the subclass is in a different package. Members with package access, however,
cannot be accessed by subclasses that are in a different package.

In Java, a reference variable is polymorphic because it can reference objects of types


different from its own, as long as those types are subclasses of its type.

The object’s type determines which method is called, not the variable’s type.

Abstract methods are used to ensure that a subclass implements the method.

An abstract class is not instantiated itself, but serves as a superclass for other classes.
The abstract class represents the generic or abstract form of all the classes that inherit
form it.

Remember the following points about abstract methods and classes:

• Abstract methods and abstract classes are defined with the abstract key word.
• Abstract methods have no body, and their header must end with a semicolon.
• An abstract method must be overridden in a subclass.
• When a class contains an abstract method, it cannot be instantiated. It must serve
as a superclass.
• An abstract class cannot be instantiated. It must serve as a superclass.

You can optionally write public in the method header of an interface, but most
programmers leave it out because all interface methods must be public.
When a class implements an interface, it is agreeing to provide all of the methods that are
specified by the interface. It is often said that an interface is like a “contract,” and when a
class implements an interface it must adhere to the contract.

An interface can contain field declarations, but all fields in an interface are treated as final
and static.

A class can extend only one superclass, but java allows a class to implement multiple
interfaces. When a class implements multiple interfaces, it must provide the methods
specified by all of them.

Third: questions
First question
public class Animal {
public void sound(){
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {


public void sound(){
System.out.println("Dog barks");
}
}
public class Test {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}

What will be the output of the above code?


A) Animal makes a sound
B) Dog barks
C) Compilation error
D) Runtime error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Second question

abstract class Shape {


abstract void draw();
}

class Circle extends Shape {


void draw() {
System.out.println("Drawing Circle");
}
}
public class Test {
public static void main(String[] args) {
Shape s = new Circle();
s.draw();
}
}

What will be the output of the above code?


A) Drawing Shape
B) Drawing Circle
C) Compilation error
D) Runtime error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Third question

interface Drawable {
void draw();
}

public class Rectangle implements Drawable {


public void draw() {
System.out.println("Drawing Rectangle");
}
}

public class Test {


public static void main(String[] args) {
Drawable d = new Rectangle();
d.draw();
}
}

What will be the output of the above code?


A) Drawing Rectangle
B) Empty output
C) Compilation error
D) Runtime error

Fourth question
class Base {
void display(){
System.out.println("Base display()");
}
}

class Derived extends Base{


void display() {
System.out.println("Derived display()");
}
}
public class Test {
public static void main(String[] args) {
Base b = new Derived();
b.display();
}
}

What will be the output of the above code?


A) Base display()
B) Derived display()
C) Compilation error
D) Runtime error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Fifth question
public class A {
public void method(){
System.out.println("Class A");
}
}
class B extends A {
public void method(){
System.out.println("Class B");
}
}

public class Test {


public static void main(String[] args) {
A a = new B();
a.method();
}
}

What will be the output of the above code?


A) Class A
B) Class B
C) Compilation error
D) Runtime error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sixth question
public class Test {
public static void main(String[] args) {
try {
int a = 5/0;
} catch (ArithmeticException e){
System.out.println("Arithmetic Exception caught");
}
}
}

What will be the output of the above code?


A) 5
B) 0
C) Arithmetic Exception caught
D) Compilation error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Seventh question
class Test {
public static void main(String[] args) {
try {
int[] arr = new int[5];
arr[10] = 50;
} catch (ArrayIndexOutOfBoundsException e){
System.out.println("Array Index Out Of Bounds Exception caught");
}
}
}

What will be the output of the above code?


A) 50
B) 0
C) Array Index Out Of Bounds Exception caught
D) Compilation error

Eighth question
interface A {
void display();
}

interface B{
void display();
}

class C implements A,B{


public void display() {
System.out.println("Display method");
}
}
public class Test {
public static void main(String[] args) {
C obj = new C();
obj.display();
}
}

What will be the output of the above code?


A) Display method
B) Empty output
C) Runtime error
D) Compilation error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ninth question
public class A {
public void method(){
System.out.println("Class A");
}
}
class B extends A {
public void method(){
System.out.println("Class B");
}
}
public class Test {
public static void main(String[] args) {
B b = new B();
b.display();
}
}

What will be the output of the above code?


A) Class A
B) Class B
C) Compilation error
D) Runtime error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. Which of the following is not a principle of Object-Oriented Programming?
A) Encapsulation
B) Abstraction
C) Polymorphism
D) Compilation
2. Which keyword is used to create a class in Java?
A) object
B) class
C) new
D) interface

3. Which keyword is used to create an object in Java?


A) object
B) class
C) new
D) instance

4. Which of the following is a valid constructor in Java?


A) public void MyClass()
B) public MyClass()
C) public static MyClass()
D) void MyClass()

5. What is the default value of a boolean variable in Java?


A) true
B) false
C) 0
D) null

6. Which of the following is not a type of inheritance in Java?


A) Single
B) Multiple
C) Multilevel
D) Hierarchical

7. What does the 'this' keyword refer to?


A) The current class
B) The current object
C) The parent class
D) The parent object

8. Which of the following statements is true about constructors?


A) Constructors can be overridden.
B) Constructors cannot be overloaded.
C) Constructors are inherited.
D) Constructors cannot have a return type.

9. Which of the following is used to access members of an object?


A) #
B) ->
C) .
D) :

10. What is the purpose of the 'super' keyword?


A) To call the parent class constructor
B) To call the current class constructor
C) To call the parent class methods
D) Both A and C

11. Which of the following concepts means wrapping data and methods together as a
single unit?
A) Inheritance
B) Polymorphism
C) Encapsulation
D) Abstraction

12. What is polymorphism in Java?


A) The ability to create a variable, a method, or an object that has more than one form
B) The process of creating a class from another class
C) The ability to hide the implementation details of a class
D) The ability to define multiple constructors for a single class

13. Which of the following allows a class to have multiple methods with the same
name?
A) Method overloading
B) Method overriding
C) Method hiding
D) Method binding
14. Which keyword is used to prevent a class from being inherited?
A) static
B) final
C) abstract
D) const

15. What is the return type of a constructor?


A) void
B) int
C) The same class type
D) Constructors do not have a return type

16. Which of the following is not a valid way to create an object in Java?
A) MyClass obj = new MyClass();
B) MyClass obj = MyClass();
C) MyClass obj = new MyClass;
D) Both B and C

17. Which of the following is true about the 'abstract' keyword?


A) Abstract classes can be instantiated.
B) Abstract methods must be defined in the subclass.
C) Abstract classes cannot have constructors.
D) Abstract classes cannot have non-abstract methods.

18. What is the correct syntax to inherit a class in Java?


A) class A extends B
B) class A inherits B
C) class A implements B
D) class A derives B

19. Which of the following methods is used to compare two objects for equality?
A) equals()
B) compareTo()
C) compare()
D) isEqual()

20. What is an interface in Java?


A) A class with all methods defined
B) A class with no methods
C) A reference type that can contain only abstract methods and constants
D) A reference type that can contain any type of methods

21. Can an interface extend another interface in Java?


A) Yes
B) No

22. What is the purpose of the 'instanceof' keyword?


A) To check if an object is an instance of a class or interface
B) To create a new instance of a class
C) To destroy an instance of a class
D) To cast an object to a different type

23. What happens if a class does not define a constructor?


A) A compilation error occurs
B) The class cannot be instantiated
C) The compiler provides a default constructor
D) The class is abstract

24. Which of the following is not a valid way to create an instance of an interface?
A) By using an anonymous class
B) By using a lambda expression (in Java 8 and above)
C) By using the 'new' keyword directly
D) By using a class that implements the interface

25. What is method overriding in Java?


A) Defining a method with the same name and parameters in a subclass
B) Defining multiple methods with the same name but different parameters in a class
C) Defining a method with the same name and parameters in the same class
D) Defining a method with the same name but different parameters in a subclass
26. Can a class implement multiple interfaces?
A) Yes
B) No

27. Which of the following is true about a static method?


A) It can access instance variables directly
B) It can be called without creating an instance of the class
C) It can override an instance method
D) It can access 'this' keyword

28. What is the purpose of the 'finalize()' method?


A) To perform cleanup before the object is garbage collected
B) To initialize an object
C) To finalize the creation of an object
D) To finalize the destruction of an object

29. Can a constructor be final in Java?


A) Yes
B) No

30. What is the use of the 'instanceof' keyword?


A) To compare the references of two objects
B) To check if an object is an instance of a class
C) To check if an object is an instance of a subclass
D) To check if an object is an instance of an interface

31. Which of the following can be static?


A) Variables
B) Methods
C) Blocks
D) All of the above

32. What is the correct way to declare an abstract class in Java?


A) class AbstractClass
B) abstract class AbstractClass
C) abstract AbstractClass
D) class abstract AbstractClass
33. Can we instantiate an abstract class?
A) Yes
B) No

34. What is an anonymous class in Java?


A) A class with no name
B) A class with no methods
C) A class with no variables
D) A class with no constructor

35. Which keyword is used to prevent a method from being overridden?


A) static
B) final
C) abstract
D) const

36. What is the return type of the hashCode() method in Java?


A) int
B) long
C) float
D) double

37. Which of the following statements is true?


A) A class can extend multiple classes
B) An interface can extend multiple interfaces
C) A class can implement multiple classes
D) An interface can implement multiple interfaces

38. Can a class be declared as both abstract and final?


A) Yes
B) No

39. Which of the following is not a type of polymorphism in Java?


A) Compile-time polymorphism
B) Runtime polymorphism
C) Method overloading
D) Constructor overloading

40. Which of the following is true about the toString() method?


A) It is a static method
B) It must be implemented by every class
C) It returns a string representation of the object
D) It is called explicitly when an object is created

41. Which of the following is a correct statement about an abstract class?


A) It cannot have any constructors
B) It can be instantiated
C) It can have both abstract and non-abstract methods
D) It cannot have any fields

42. What does the 'final' keyword mean when applied to a method?
A) The method cannot be called
B) The method cannot be inherited
C) The method cannot be overridden
D) The method cannot be accessed outside its class

43. Which of the following is not a feature of an abstract class?


A) It can be extended by other classes
B) It can be instantiated
C) It can contain abstract methods
D) It can contain non-abstract methods

44. What is the correct way to declare an interface in Java?


A) interface MyInterface {}
B) class MyInterface implements Interface {}
C) abstract class MyInterface {}
D) public interface MyInterface() {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Model Answer
1) D
2) B
3) C
4) B
5) B
6) B
7) B
8) D
9) C
10) D
11) C
12) A
13) A
14) B
15) D
16) D
17) B
18) A
19) A
20) C
21) A
22) A
23) C
24) C
25) A
26) A
27) B
28) A
29) B
30) B
31) D
32) B
33) B
34) A
35) B
36) A
37) B
38) B
39) D
40) C
41) C
42) C
43) B
44) A

Programming Midterm Exam

1) The keyword_______is required to declare a class.


A) private
B) public
C) class
D) All of the above.

2) ______ is invoked to create an object.


A) A method with a return type
B) The main method
C) A method with the void return type
D) A constructor

3) Given the declaration Circle x = new Circle(), which of the following statement is most
accurate?
A) x contains a reference to a Circle object.
B) x contains an object of the Circle type.
C) x contains an int value.
D) You can assign an int value to x.

4) represents an entity in the real world that can be distinctly identified.


A) A class B) A data field
C) A method D) An object

5) Variables are classified according to their:


A) Value.
B) Names.
C) Data type.
D) Location in the program.

6) _______ is a construct that defines objects of the same type.


A) A class B) A data field
C) A method D) An object

7) Object-oriented programming allows you to derive new classes from existing classes. This is
called ____.
A) abstraction B) inheritance
C) generalization D) encapsulation

8) Values that are sent into a method are called_____.


A) literals B) variables
C) types D) arguments

9) What key word can you use to call a superclass


constructor explicitly?
A) super B) goto
C) this D) extends

10) You use the________operator to access members of an object.


A) . B) * C) () D) %

11) Inheritance through an extended (derived) class supports which of the following concepts?
A) interfaces B) code reuse
C) correctness D) modulary

12) If ClassA extends ClassB, then:


A) Public and private members of ClassB are public and private, respectively, in ClassA.
B) Private members in ClassB are changed to protected members in ClassA.
C) Neither public nor private members in
ClassB can be directly accessed in
ClassA.
D) Public members in ClassB are public in ClassA, but private members in ClassB cannot be
directly accessed in ClassA.

13) Replacing inadequate superclass methods with more suitable subclass methods is known as what?
A) Method overriding
B) Method overloading
C) Method upgrading
D) Tactical inheritance

14) If two methods have the same name but different signatures, they are:
A) Overloaded.
B) Superclass methods.
C) Overridden.
D) Subclass methods.

15) Which of the following is not a benefit derived from using methods in programming?
A) Simplifies programs
B) Code reuse
C) Problems are more easily solved.
D) All of the above are benefits.

16) This part of a method is a collection of statements that are performed when the method is
executed.
A) Method header
B) Method modifier
C) Return type
D) Method body

17) Which of the following is not part of a method call?


A) Method name
B) Return type
C) Parentheses
D) All of the above are part of a method call.

18) If method A calls method B, and method B calls method C, and method C calls method D, when
method D finishes, what happens?
A) The program terminates.
B) Control is returned to method C.
C) Control is returned to method B.
D) Control is returned to method A.

19) One or more objects may be created from a(n):


A) Instance. B) Class.
C) Field. D) Method.

20) ________is invoked to create an object.


A) A constructor
B) A method with the void return type
C) The main method
D) A method with a return type

21) Inheritance means


A) that data fields should be declared private
B) that a class can extend another class
C) that a variable of supertype can refer to a subtype object
D) that a class can contain another class

22) This refers to the combining of data and code into a single object.
A) Encapsulation B) Object
C) Data hiding D) Abstraction

23) Which of the following can be placed in the blan line in the following code?
Public class Test {
private int id;
Public void something() {
_________.id = 45; }}
A) this B) Test
C) self D) This

24) In UML diagrams, this symbol indicates that a member is private:


A) * B) + C) - D) #

25) What is stored by a reference variable?


A) A memory address
B) A string
C) A binary encoded decimal
D) An object

26) It is common practice in object‐oriented programming to make all of a classʹs:


A) Methods private.
B) Fields and methods public.
C) Fields public.
D) Fields private.

27) This is a group of related classes.


A) Package B) Archive
C) Collection D) Attachment
28) Quite often you have to use this statement to make a group of classes available to a program.
A) link B) use
C) assume D) import

29) Suppose you create a class Cylinder to be a subcl of Circle. Analyze the following code:

class Cylinder extends Circle { double length;

Cylinder(double radius) { Circle(radius);


}
}
A) The program has a compile error because you attempted to invoke the Circle classʹs
constructor illegally.
B) The program compiles fine, but it has a runtime error because of invoking the Circle
classʹs constructor illegally.
C) The program compiles fine, but you cannot create an instance of Cylinder because the
constructor does not specify the length of the cylinder.

30) In the following code,


Integer.parseInt(str) , is an example of
______.

int num;
string str = "555";
num = Integer.parseInt(str) + 5;

A) a complex method
B) a void method
C) a local variable
D) a value‐returning method

Answer Key
Testname: ZNU_OOP_AI_M1_MID_2024

1) C
2) D
3) A
4) D
5) C
6) A
7) B
8) D
9) A
10) A
11) B
12) D
13) A
14) A
15) D
16) D
17) B
18) B
19) B
20) A
21) B
22) A
23) A
24) C
25) A
26) D
27) A
28) D
29) A
30) D

You might also like