100% found this document useful (1 vote)
79 views11 pages

OOP Concepts and MCQs for Assessment

This document contains a 15 question multiple choice quiz on topics related to object oriented programming in Java. The questions cover concepts like constructors, exceptions, inheritance, interfaces, abstract classes, packages and design patterns. The document instructs students to choose one answer for each question unless otherwise specified and that each question is worth 3 marks.

Uploaded by

Ahmad Azam
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
79 views11 pages

OOP Concepts and MCQs for Assessment

This document contains a 15 question multiple choice quiz on topics related to object oriented programming in Java. The questions cover concepts like constructors, exceptions, inheritance, interfaces, abstract classes, packages and design patterns. The document instructs students to choose one answer for each question unless otherwise specified and that each question is worth 3 marks.

Uploaded by

Ahmad Azam
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • SECTION A: Multiple Choice Questions of Different Topics
  • SECTION B: Java Collections of Objects
  • SECTION C: Java Classes and OO Concepts, UML
  • SECTION D: File I/O and Tests, Exceptions

CS1OOP Object Oriented Programming 2020-21

A SECTION A: Multiple Choice Questions of Different


Topics

INSTRUCTIONS: This section contains 15 questions that are worth 3 marks


each. Please answer ALL questions. Choose only one option unless something
else is said.

1. Given any constructor. Which one of these statements is the appropriate answer?

(3 marks)

a) It must have the same name as the class it is declared within.

b) It is used to create objects.

c) It may be declared private.

d) Both (A) and (B) above.

e) (a), (b) and (c) above.

2. Which of the following is true?

a) A finally block is executed before the catch block but after the try block.

b) A finally block is executed, only after the catch block is executed.

c) A finally block is executed whether an exception is thrown or not.

d) A finally block is executed, only if an exception occurs.

e) None of the above.

1 OF 11
This assessment is subject to the University Assessment Regulations for Candidates
CS1OOP Object Oriented Programming 2020-21

3. Which one of the following statements is NOT true? (3 marks)

a) A class containing abstract methods is called an abstract class.

b) Abstract methods should be implemented in the derived class.

c) An abstract class cannot have non-abstract methods.

d) A class must be qualified as an ‘abstract’ class, if it contains one abstract


method.

e) None of the above

4. You are writing a generic method which is going to replace all the elements that
are below a certain value with that value. Which is the most generic declaration
that you can use for it?
(3 marks)

a) public <T extends Comparable<T> > void replaceSmaller(T t, List<? extends


T> l)

b) public <T extends Comparable<T> > void replaceSmaller(T t, List<? super T>
l)

c) public <T extends Comparable<T> > void replaceSmaller(T t, List<T> l)

d) public <T extends Comparable<T> > void replaceSmaller(T t, List<?> l)

e) None of the above apply

2 OF 11
This assessment is subject to the University Assessment Regulations for Candidates
CS1OOP Object Oriented Programming 2020-21

5. To prevent any method from overriding. How should the method be declared?

(3 marks)

a) static

b) final

c) abstract

d) constant

e) none of the above

6. Suppose that we have the classes Animal and Bird, and Animal defines an eat()
method that Bird overrides. We have an Animal variable named "a" that points
to a Bird object. When we run "[Link]()", which version of the method gets
called?"

a) The one in Animal

b) The one in Bird

c) Compiler will decide randomly

d) Interpreter will decide randomly

e) None of the above

3 OF 11
This assessment is subject to the University Assessment Regulations for Candidates
CS1OOP Object Oriented Programming 2020-21

7. Your program needs to quickly find the student number of a Student by their
name, and you want to display all the names in alphabetical order.

Which JCF collection should you use? (3 marks)

a) HashMap<String, Student>

b) TreeMap<String, Student>

c) ArrayList<[Link]<String, Student> >

d) LinkedHashMap<String, Student>

e) None of the above

8. Which one of these statements is correct? (3 marks)

a) The dynamic type is what the compiler uses to check that we are calling ap-
propriate methods and returning/passing valid values.

b) The static type of a variable is not known until we run the program.

c) instanceof checks the dynamic type of a variable.

d) A cast changes the dynamic type of a variable.

e) None of the above.

4 OF 11
This assessment is subject to the University Assessment Regulations for Candidates
CS1OOP Object Oriented Programming 2020-21

9. We are designing a class that keeps a list of objects always sorted after each
insertion. We want to add an internal check in the class that we can enable and
disable without recompiling, which verifies that the list remains sorted and has
exactly one more element after each insertion.

Which approach should we take? (3 marks)

a) Do a check and throw an unchecked exception if not met

b) Do a check and throw a checked exception if not met

c) Use assert

d) Use assertTrue

e) None of the above

10. Consider the following statements about Java packages:

I. Packages don’t provide a mechanism to partition all class names into more
manageable chunks.
II. One of the important properties of a package is that all classes defined inside
a package is accessible by code outside that package.
III. Packages provide a visibility control mechanism.
IV. The .class files for classes declared to be part of a package can be stored in
multiple directories.

Which one of them is correct? (3 marks)

a) Only (I)

b) Only (II)

c) Only (III)

d) Only (IV)

e) All (I), (II), (III) and (IV) above are wrong.

5 OF 11
This assessment is subject to the University Assessment Regulations for Candidates
CS1OOP Object Oriented Programming 2020-21

11. Given this code:


1 public interface MyInterface {
2 void foo() throws MyException;
3 }

Select the ONE correct statement below: (3 marks)

a) MyException extends Exception, and it is a checked exception.

b) MyException extends RuntimeException, and it is an unchecked exception.

c) MyException extends Exception, and it is an unchecked exception.

d) MyException extends RuntimeException, and it is a checked exception.

e) None of the above statements are correct.

12. Which one of the following is TRUE?: (3 marks)

a) A class has always a constructor (possibly automatically supplied by the Java


compiler).

b) In Java, an instance field declared public generates a compilation error.

c) int is the name of a class available in the package [Link]

d) Instance variable names may only contain letters and digits.

e) The more comments in a program, the faster the program runs.

6 OF 11
This assessment is subject to the University Assessment Regulations for Candidates
CS1OOP Object Oriented Programming 2020-21

13. Given this code:


Beginning of code
 public abstract class Shape
 {
 //stuff about shapes
 }
 public class Square extends Shape
 {
 //Square
 }
End of code

Which of the following assignments are legal? (3 marks)

a) Shape s1 = new Square();

b) Square q1 = new Shape();

c) Rectangle e1 = Square();

d) Rectangle e1 = Square(); and Square q1 = new Shape();

e) None of the above

14. Suppose that we have written a Dictionary called SimilarWordDictionary,


which stores words and their definitions. We want to be able to give different
ways of determining whether two words are similar or not (i.e. ignoring one ty-
pographical error, or ignoring lowercase/uppercase). We want to be able to add
those different ways without changing SimilarWordDictionary.

Which one of these patterns would best suit this context? (3 marks)

a) Strategy

b) State

c) Template Method

d) Decorator

e) None of the above

7 OF 11
This assessment is subject to the University Assessment Regulations for Candidates
CS1OOP Object Oriented Programming 2020-21

15. Given this Java code:


Beginning of code
 interface A {
 void f(int x);
 }
 interface B extends A {
 default void g() { f(2); }
 }
 interface C extends A {
 default void g() { f(3); }
 }
 interface D extends B, C {
 // ... conflicting g ...
 }
End of code

What do you need to do to solve the conflict ? S ELECT ALL valid options you find.

(3 marks)

a) There is no conflict

b) I would propose this solution


Beginning of code
 @Override default void g() {
 [Link].g();
 }
End of code

c) I would propose this solution


Beginning of code
 @Override default void g() {
 [Link].g();
 }
End of code

d) I would propose this solution


Beginning of code
 @Override default void g() {
 [Link].g();
 }
End of code

e) None of the above

[45 marks]

8 OF 11
This assessment is subject to the University Assessment Regulations for Candidates
CS1OOP Object Oriented Programming 2020-21

B SECTION B: Java Collections of Objects

This question is about the use of an ArrayList or a HashMap to store a collection of


Clothingitems for a shop.

16. W HAT are the differences between an ArrayList and a HashMap?


(4 marks)

17. W RITE a class ListCollection to use an ArrayList to store a collection of ClothingItems


AND WRITE a class MapCollection to use a HashMap to manage a collection of
ClothingItems.

a) Each of these two classes should have the following:


i) A constructor to create the empty collection
ii) A method to add a ClothingItem to the collection
iii) A method to search for and return a Clothingitem by its ID
iv) A method to search for and return a list of Clothingitems that have the
same name (ie, find all the T-Shirt items)
(16 marks)
b) You need to WRITE the class to represent a Clothingitem. The ClothingItem
class defines the following fields: a Unique id (an int), a name, a colour both
(String) and a price (float).
(5 marks)

[25 marks]

9 OF 11
This assessment is subject to the University Assessment Regulations for Candidates
CS1OOP Object Oriented Programming 2020-21

C SECTION C: Java Classes and OO concepts, UML

18. Given this UML diagram:

a) W RITE the Java code for a skeleton of CheckAccount that fully specifies the
getSortCode, getAccountNumber and getTransactions methods but,
leave empty the getBalanceAt and drawBalanceChart methods.

(5 marks)

b) B RIEFLY PROVIDE EXAMPLES of how the principles of abstraction, encapsula-


tion and implementation hiding are represented in the class CheckAccount.

(3 marks)

c) W RITE a Javadoc comment for the getBalanceAt method. Make sure you
describe the parameter and the returned value, and also document how the
method may throw an IllegalArgumentException if given a null Date.

(4 marks)

d) i) W RITE what is the UML relationship between CheckAccount and Transaction?


ii) E XPLAIN why you think the UML relationship chosen was a good choice or
not.
(3 marks)

[15 marks]

10 OF 11
This assessment is subject to the University Assessment Regulations for Candidates
CS1OOP Object Oriented Programming 2020-21

D SECTION D: File I/O and Tests, Exceptions

19. W RITE a main method that reads all the lines of a file, and then writes those
to another file in reverse order. Print appropriate error messages if a FileNot-
FoundException or an IOException is thrown.
(6 marks)

19. C OMPARE black-box and white-box testing, and EXPLAIN their differences. E X -
PLAIN how we know if we are missing tests in white-box testing.
(3 marks)

20. What is the difference between Long and long in Java? (3 marks)

21. Given this code:


Beginning of code
 Dog d1 = new Dog("Fido", 1);
 for (int i = 0; i < 10; i++) {
 d1 = new Dog("D" + i, i);
 }
 Dog d2 = d1;
End of code

• How many Dog objects are created?


• How many Dog objects remain after running the last line?
• What is the relationship between d1 and d2?

(3 marks)

[15 marks]

[Total Exam 100 marks]

END OF ASSESSMENT

11 OF 11
This assessment is subject to the University Assessment Regulations for Candidates

Common questions

Powered by AI

Interfaces with default methods provide a mechanism to add new methods to interfaces without breaking existing implementations. In the context of interface D, which extends both interfaces B and C with conflicting default methods, resolving method conflicts involves explicitly specifying which super interface's method should be used. This grants flexibility in method inheritance and avoids forcing all implementing classes to provide their own implementation .

The code involving the Dog class example creates 11 Dog objects, where each iteration of the loop creates a new Dog object and reassigns the reference, causing previous objects to be eligible for garbage collection. After the last line, only one Dog object remains, referred to both by d1 and d2, illustrating how Java manages references and garbage collection to maintain memory efficiency .

In a UML context, CheckAccount might have a 'composition' relationship with Transaction if the lifetime of the transactions is tightly bound to that of the CheckAccount, indicating ownership. Conversely, an 'association' might be more appropriate if Transactions exist independently. Composition is favored when transactions do not logically exist without the account holding them, emphasizing encapsulation and a stronger tie between the objects .

The 'finally' block in Java exception handling is used to execute code after a try-catch block, regardless of whether an exception was thrown or not, thus ensuring the execution of crucial cleanup code. Unlike catch blocks, which only execute if an exception of a specified type is thrown, a 'finally' block provides execution guarantees except in cases of JVM crashes or 'System.exit()' calls .

Declaring a method as 'final' in Java prevents any subclass from overriding it. This enforces the implementation of the method in the superclass, ensuring consistent behavior across all subclasses .

An ArrayList can be used to store a simple collection of ClothingItems, which allows for convenient iteration and access via indices, making it suitable for features like listing all items. A HashMap, on the other hand, can manage ClothingItems by a unique ID, enabling efficient retrieval and modification. While the ArrayList is better for ordered data access, the HashMap excels in quick lookups and modifications of items by their key .

The main method would use FileReader and BufferedReader to read lines from a file into a List, then utilize Collections.reverse() to reverse the lines. Using FileWriter and BufferedWriter, the reversed lines would be written to another file. Exception handling should include try-catch blocks for FileNotFoundException and IOException, providing error messages to handle any I/O issues gracefully .

The static type of a variable is determined at compile-time, specifying the type which the variable must conform to. The dynamic type, however, is determined at runtime and represents the actual class of the object the variable is referencing . The compiler checks the static type for method calls and assignments, while the dynamic type is used at runtime for method dispatching and instanceof checks .

A TreeMap supports quick lookup and maintains its keys in sorted order, which is advantageous when both sorted data and efficient retrieval are required. In contrast, a HashMap provides constant-time performance for basic operations but does not maintain any order of its keys .

Black-box testing focuses on testing the software's external behavior without any knowledge of internal implementation, primarily effective for validating functionality against requirements. White-box testing involves examining the internal structures and can reveal how calculations are performed or where potential logical errors exist, being more effective for assessing code quality and correctness of processes. Missing tests in white-box testing can be identified by covering all possible execution paths .

CS1OOP Object Oriented Programming 2020-21
A
SECTION A: Multiple Choice Questions of Different
Topics
INSTRUCTIONS: This sect
CS1OOP Object Oriented Programming 2020-21
3.
Which one of the following statements is NOT true?
(3 marks)
a) A class contain
CS1OOP Object Oriented Programming 2020-21
5.
To prevent any method from overriding. How should the method be declared?
(3 ma
CS1OOP Object Oriented Programming 2020-21
7.
Your program needs to quickly find the student number of a Student by their
name
CS1OOP Object Oriented Programming 2020-21
9.
We are designing a class that keeps a list of objects always sorted after each
CS1OOP Object Oriented Programming 2020-21
11.
Given this code:
1 public interface MyInterface {
2
void foo() throws MyExcept
CS1OOP Object Oriented Programming 2020-21
13.
Given this code:
Beginning of code

public abstract class Shape

{

//stuff
CS1OOP Object Oriented Programming 2020-21
15.
Given this Java code:
Beginning of code

interface A {

void f(int x);

}

CS1OOP Object Oriented Programming 2020-21
B
SECTION B: Java Collections of Objects
This question is about the use of an Arra
CS1OOP Object Oriented Programming 2020-21
C
SECTION C: Java Classes and OO concepts, UML
18.
Given this UML diagram:
a) WRIT

You might also like