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

OOP Concepts Assessment 2023/2024

Uploaded by

Djamen
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 views2 pages

OOP Concepts Assessment 2023/2024

Uploaded by

Djamen
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

CATHOLIC UNIVERSITY OF CAMEROON (CATUC), BAMENDA

P.O. Box 782 Bamenda, Cameroon

FIRST SEMESTER CONTINUOUS ASSESSMENT 2023/2024 SESSION

LECTURER: DJAMEN NYONKEU Gildas

COURSE TITLE: OOP 1


COURSE CODE : CSC2104 DURATION : 1h00

Question 1 (10 marks)


Fill each of the following blanks with one of the words given in the list below: [1 mark each]
{dynamic, polymorphism, protected, interface, override, objects, inheritance, static, tokenizing, super}
1- …………. are used to model things from a problem domain.
2- The term ………………… refers to the fact that a variable can hold objects of different types.
3- In an ………………. all methods are abstract.
4- The keyword ……………… is used to call methods in the superclass.
5- …………….. is an access control keyword that permits access from the subclass, but denies access from
anywhere not in a class or its subclass.
6- To cut up an input string into separate words is called ………………..
7- …………….. allows us to define one class as an extension of another.
8- A subclass can …………….. a superclass method by declaring a method with the same signature as the
superclass method.
9- The class variable is declared as ……………..
10- The ……………… type of a variable is the type of the object that is currently stored in the variable.
Question 2 (10 marks)
(a) What is wrong with the following class definitions? How would you fix them? [3 marks]
class Employee { private String name;
private int id;
public Employee (String name, int id)
{ [Link] = name; [Link] = id; }
public String getName ( ) { return name; }
public int getId ( ) { return id; }
}
class Manager extends Employee
{ private String department;
public Manager (String name, int id, String department)
{ [Link] = name; [Link] = id; [Link] = department;}
public void getDeprtment ( ) { return department; }
}
(b) What does this piece of code do? [3 marks]
Random rand = new Random ( );
String table = "";
String row = "";
for (int i = 1; i < 4; i++)
{ for (int j = 1; j < 4; j++)
{ int number = [Link](50));
row += number + " ";
}
table += row + "\n";
row = "";
}
[Link](table);
(c) What is wrong with the following interface? Correct it. [2 marks]
public interface Figures
{ public void printMessage (String s)
{ [Link] ("This figure is " + s ); }
public double area ( ) ; // to calculate the area of the figure
}
(d) Design a class Rectangle that implements the corrected interface of section (c). [2 marks]
Question 3) (5 marks)
1) One of the following is NOT TRUE about Object-Oriented Paradigms (OOPs):
A) OOP is a set of techniques and processes focusing on how to analyse and model a real world problem
and to design a solution.
B) The intended benefits of OOP are to solve the “software” crisis, break complexity into small
manageable chunks, and make software maintenance easier.
C) OOP allows reuse of components – plug and play
D) OOP solves the entire problem in one program.
2) Which point is FALSE from the following?
A) A class is an object
B) A class is a template or prototype that defines the composition and the behavior of all objects of
certain kinds.
C) A class may have fields of composite types
D) From a class you may initiate an object.
3) Methods of a class are invoked from outside the class by
A) objects using the dot notation. e.g. circle_1.moveHorizontal (50)
B) using its name only e.g. moveHorizontal (50)
C) using the call statement e.g. CALL moveHorizontal (50)
D) A and B above
4) A method in a class that is used to change the values of some fields in that class is called:
A) A constructor
B) An accessor method
C) A mutator method
D) None of the above
5) If there are one or more constructors for a class then
A) Exactly one of the constructors will be called each time an object of that class is created
B) All of the constructors will be called each time an object of that class is created
C) A destructor must also be written.
D) None of the above, classes cannot have constructors
6) Which Feature of OOP illustrated the code reusability?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Inheritance
7) Which of the following option leads to the portability and security of Java?
a. Bytecode is executed by JVM
b. The applet makes the Java code secure and portable
c. Use of exception handling
d. Dynamic binding between objects
8) Evaluate the following Java expression, if x=3, y=5, and z=10:
++z + y - y + z + x++
a. 24 b. 23 c. 20 d. 25

Common questions

Powered by AI

The result of the expression '++z + y - y + z + x++' is 25. Initially, z is incremented to 11 with ++z, making the expression 11 + 5 - 5 + 11 + 3. This gives (11) + (5 - 5) + (11) + (3) = 25. 'x++' evaluates to 3 during this computation but becomes 4 after expression evaluation .

Encapsulation restricts direct access to some of an object's components, protecting the integrity of its data by allowing controlled interaction through methods. This encapsulated approach ensures that the object's internal state cannot be altered directly, thus promoting data integrity and security .

Inheritance allows a new class to inherit fields and methods from an existing class, promoting code reuse and a hierarchical class structure. This facilitates the extension of existing code without altering original classes, enabling an efficient way to manage hierarchical relationships .

Statement D, "OOP solves the entire problem in one program," is not true. OOP focuses on breaking down complex problems into smaller, reusable components (objects), rather than solving a problem entirely within a single program, promoting modularity and maintainability .

Inheritance is the feature of OOP that facilitates code reusability, as it allows for defining one class based on another existing class, enabling the reuse of shared code across multiple classes .

The Java Virtual Machine (JVM) enhances portability by executing bytecode, which is platform-independent, ensuring that Java programs can run on any device with a JVM. It provides security by isolating Java programs from direct interaction with the host system, reducing vulnerabilities .

The Java code initializes a 3x3 table of random integers between 0 and 49. For each loop iteration, it creates a row of three numbers and adds it to the table as a string, resulting in a table of random integers being printed as a formatted string .

The Manager class incorrectly attempts to access the private fields 'name' and 'id' from the superclass Employee directly, which is not allowed. This can be corrected by calling the superclass constructor using 'super(name, id)' to properly initialize these fields. Additionally, the method 'getDeprtment()' has a typo and should be 'getDepartment()' .

The interface 'Figures' incorrectly includes a method with an implementation, which is not allowed in Java interfaces that should only contain abstract methods. The method 'printMessage' should not have a body; it should be declared abstract or moved to an implementing class .

Polymorphism enhances flexibility by allowing variables to hold objects of different types, permitting methods to behave differently based on the object type being referenced. This enables the same interface to be used for functions of different classes, promoting extensibility and scalability in code .

You might also like