Programming
Programming
First: definitions
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.
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.
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.
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.
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.
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.
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).
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.
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.
object class: the java API has a class named object which all other classes directly or
indirectly inherit form.
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.
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.
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.
• 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.
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.
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.
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.
• 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");
}
}
interface Drawable {
void draw();
}
Fourth question
class Base {
void display(){
System.out.println("Base display()");
}
}
Eighth question
interface A {
void display();
}
interface B{
void display();
}
11. Which of the following concepts means wrapping data and methods together as a
single unit?
A) Inheritance
B) Polymorphism
C) Encapsulation
D) Abstraction
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
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
19. Which of the following methods is used to compare two objects for equality?
A) equals()
B) compareTo()
C) compare()
D) isEqual()
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
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
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.
7) Object-oriented programming allows you to derive new classes from existing classes. This is
called ____.
A) abstraction B) inheritance
C) generalization D) encapsulation
11) Inheritance through an extended (derived) class supports which of the following concepts?
A) interfaces B) code reuse
C) correctness D) modulary
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
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.
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
29) Suppose you create a class Cylinder to be a subcl of Circle. Analyze the following code:
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