CS211 Summer2024 Midterm B1 A
CS211 Summer2024 Midterm B1 A
Exam distribution (100 pts): TF (10 pts), MC (60 pts), *.java (30 pts)
This is a closed notes exam, do not omit paperwork, id all your pages.
a) In Java, one of the first things you encounter is the concept of variables and their accessibility. For instance,
when a variable is declared with the static keyword, it signifies that there is a single copy of this variable
for all instances of the class. [ True False ]
b) Continuing the theme of classes, you might think that defining a class in Java automatically creates an instance
of it, but in reality, a class definition simply outlines the structure and behaviors of objects. An instance is
created only when you use the final keyword. [ True False ]
c) In defining methods, you'll come across various return types. The new keyword is used in method declarations
to specify that the method does not return any value. It's like saying "this method performs some actions, but
it doesn't give anything back." [ True False ]
d) Arrays in Java are objects that hold a fixed number of values of a single type. The length of an array is
established when the array is created and cannot be altered. This means that if you need an array with a
different size, you must change that array’s reference for new one. [ True False ]
e) Java's array structures are quite flexible. An array in Java can hold other arrays, and these nested arrays may
have the same length or different lengths. [ True False ]
f) In Java, encapsulation is a key principle. A static method in a class is not accessible from outside that class.
However, within the same class, a private method can be called by any other method, including those with
final access. [ True False ]
g) To control the flow in loops, the continue statement is used within loops to exit the entire loop early, not
just to skip the current iteration and continue with the next one. For skipping the current iteration, return is
the keyword you would use. [ True False ]
h) When creating objects in Java, it is essential to understand what gets shared and what is unique to each
instance. Each object does not get its own copy of the class methods; instead, all instances share the single set
of methods defined in the class unless overloaded. [ True False ]
i) When dealing with class methods, the final methods are tied to the class, not instances of the class. These
methods can access other static methods and static variables without creating an instance of the class,
but they cannot call instance methods directly. [ True False ]
j) Lastly, when you're working within a class, there are variables you create called members (fields and methods).
These variables are not visible outside the method, which is why they do not have access modifiers like
public , private , or protected . [ True False ]
All students in CS classes must adhere to the GMU Honor Code. In addition to this honor code, the computer science department has further honor code policies detailed below regarding ALL
kinds of work for classes in the CS major, including programming projects, written text and reports, diagrams, pseudocode, and so on. ONLY ONE WARNING WILL BE GIVEN AS A PAPER MARK.
George Mason University | School of Computing | Computer Science Department
1
NAME: ___________________ G#: __________________
CS211 SUMMER 2024 MIDTERM EXAM
System.out.println(Integer.MIN_VALUE - 1);
2147483647
-2147483648
2147483648
Type Error
Reference Error
3. What is printed by the following Java code snippet given this documentation?
All students in CS classes must adhere to the GMU Honor Code. In addition to this honor code, the computer science department has further honor code policies detailed below regarding ALL
kinds of work for classes in the CS major, including programming projects, written text and reports, diagrams, pseudocode, and so on. ONLY ONE WARNING WILL BE GIVEN AS A PAPER MARK.
George Mason University | School of Computing | Computer Science Department
2
NAME: ___________________ G#: __________________
CS211 SUMMER 2024 MIDTERM EXAM
7. For precise calculaPons, like in astronomy, which Java type is best? (e.g., Measuring distances)
float
imaginary
BigDecimal
long
double
8. Which Java type is designed for true/false condiPons, and can it be 'null'? (e.g., Checking if even)
boolean: Cannot be 'null'
Boolean: Can be 'null'
Null: Can be 'null'
Boolean: Cannot be 'null'
boolean: Can be 'null'
9. What's the highest value a Java integer can store without overflow? (e.g., Storing populaPon count)
2147483647
9223372036854775807
32767
-2^31
127
10. For high precision in finance, which Java primiPve type and literal indicaPon is used? (e.g., Monetary values)
double: No suffix
float: 'f' or 'F'
long: 'L' or 'l'
short: 'S' or 's'
BigDecimal: Not applicable
All students in CS classes must adhere to the GMU Honor Code. In addition to this honor code, the computer science department has further honor code policies detailed below regarding ALL
kinds of work for classes in the CS major, including programming projects, written text and reports, diagrams, pseudocode, and so on. ONLY ONE WARNING WILL BE GIVEN AS A PAPER MARK.
George Mason University | School of Computing | Computer Science Department
3
NAME: ___________________ G#: __________________
CS211 SUMMER 2024 MIDTERM EXAM
14. Which term describes a collecPon of objects with similar responsibiliPes within an object-oriented system?
(e.g., All savings accounts in a banking applicaPon)
Method
Class
State
AcPvity
Package
15. How would you explain 'behavior' in object-oriented modeling? (e.g., A printer's ability to print or scan a document)
The object's relaPonships with other objects
The object's state changes over Pme
The operaPons or funcPons the object can perform
The staPc properPes of the object
The graphical representaPon of the object
16. What concept in object modeling represents the state and the behavior that an object encapsulates? (e.g., An
invoice with line items and the total amount calculaPon)
Interface
Class
Package
Component
Node
17. Which principle refers to the concept that an object should hide its internal state and require all interacPon to
be performed through its methods? (e.g., Accessing account balance through a secure method)
EncapsulaPon
AbstracPon
Inheritance
Polymorphism
ComposiPon
18. Which object-oriented abstracPon involves designing objects to share behaviors and to be treated
polymorphically? (e.g., Different types of employees processed in a payroll system)
EncapsulaPon
AbstracPon
Inheritance
Polymorphism
AggregaPon
19. When analyzing the problem domain for object modeling, which technique would you use to categorize and
group elements based on their common characterisPcs? (e.g., Grouping different types of bank accounts)
AbstracPon
EncapsulaPon
AggregaPon
Inheritance
ClassificaPon
All students in CS classes must adhere to the GMU Honor Code. In addition to this honor code, the computer science department has further honor code policies detailed below regarding ALL
kinds of work for classes in the CS major, including programming projects, written text and reports, diagrams, pseudocode, and so on. ONLY ONE WARNING WILL BE GIVEN AS A PAPER MARK.
George Mason University | School of Computing | Computer Science Department
4
NAME: ___________________ G#: __________________
CS211 SUMMER 2024 MIDTERM EXAM
20. In object modeling, what does 'polymorphism' allow objects to do within a system? (e.g., ExecuPng different
code paths for a document's Print command based on the document type)
Share the same interface or class base
Change their a=ributes during run>me
Respond differently to the same method call
Maintain mul>ple states simultaneously
Perform mul>ple opera>ons in parallel
All students in CS classes must adhere to the GMU Honor Code. In addition to this honor code, the computer science department has further honor code policies detailed below regarding ALL
kinds of work for classes in the CS major, including programming projects, written text and reports, diagrams, pseudocode, and so on. ONLY ONE WARNING WILL BE GIVEN AS A PAPER MARK.
George Mason University | School of Computing | Computer Science Department
5
NAME: ___________________ G#: __________________
CS211 SUMMER 2024 MIDTERM EXAM
26. When designing a base class that specifies a method must be implemented by subclasses but does not
provide a default implementaPon, what keyword do you use?
public abstract
protected abstract
private abstract
staPc abstract
final abstract
27. When declaring a class that should not be instanPated directly but can have subclasses, which keyword must
the class definiPon include?
public abstract
private abstract
final abstract
protected abstract
staPc abstract
28. In a class, you have a field that must be accessible from anywhere, but its iniPal value should be set only once
during the runPme in a staPc block. What is the correct way to declare this field?
public staPc final
public staPc
protected staPc final
staPc final
final staPc volaPle
29. You need to design a method that can access properPes of the extending class and modify them. Which
keyword should the method in the super class NOT have?
abstract
staPc
default
final
None of the above
30. A developer wants to ensure that a complex calculaPon within a class can be reused by different methods of
the class but should not be visible or accessible to any other class, not even subclasses. How should this
method be declared?
private final
protected final
public final
private staPc
private
All students in CS classes must adhere to the GMU Honor Code. In addition to this honor code, the computer science department has further honor code policies detailed below regarding ALL
kinds of work for classes in the CS major, including programming projects, written text and reports, diagrams, pseudocode, and so on. ONLY ONE WARNING WILL BE GIVEN AS A PAPER MARK.
George Mason University | School of Computing | Computer Science Department
6
NAME: ___________________ G#: __________________
CS211 SUMMER 2024 MIDTERM EXAM
< Model it with the UML diagram (box with three sections):
<> Provide a Java class implementing the model (your usual *.java file):
All students in CS classes must adhere to the GMU Honor Code. In addition to this honor code, the computer science department has further honor code policies detailed below regarding ALL
kinds of work for classes in the CS major, including programming projects, written text and reports, diagrams, pseudocode, and so on. ONLY ONE WARNING WILL BE GIVEN AS A PAPER MARK.
George Mason University | School of Computing | Computer Science Department
7
NAME: ___________________ G#: __________________
CS211 SUMMER 2024 MIDTERM EXAM
// 0. USE THE MAIN METHOD TO REASON ABOUT THE PROBLEM, THESE ARE INPUT VALUES USED BY THE CONSTRUCTOR
// ONLY CONSIDER THESE CASES FOR VALUES (VARIABLE)
// THE ENTRY OF THE ARRAY values CONTAINS ARRAYS OF EITHER 1 OR TWO ELEMENTS ONLY.
public static void main(String[] args) {
int[][] values = { { 1 }, { 1, 1 }, {2}, { 2, 3 }, { 3, 3 } };
ShapeManager sm = new ShapeManager(values);
}
}
// 1.A IMPLEMENTING TYPES ONLY CARE TO RETURN A CALCULATION OF WHAT THEY USE TO DO
// (AREAS FOR THE CASE OF SHAPES)
interface Calculable {
public int calculate();
}
All students in CS classes must adhere to the GMU Honor Code. In addition to this honor code, the computer science department has further honor code policies detailed below regarding ALL
kinds of work for classes in the CS major, including programming projects, written text and reports, diagrams, pseudocode, and so on. ONLY ONE WARNING WILL BE GIVEN AS A PAPER MARK.
George Mason University | School of Computing | Computer Science Department
8
NAME: ___________________ G#: __________________
CS211 SUMMER 2024 MIDTERM EXAM
// 1.C SETUP THE CLASS METHODS, THE AREA OF N CUBE IS CALCULATED MULTIPLYING ITS SIDE N TIMES
public int getArea() {
Imaginarium (1 question)
What would you have liked to have on the exam?
All students in CS classes must adhere to the GMU Honor Code. In addition to this honor code, the computer science department has further honor code policies detailed below regarding ALL
kinds of work for classes in the CS major, including programming projects, written text and reports, diagrams, pseudocode, and so on. ONLY ONE WARNING WILL BE GIVEN AS A PAPER MARK.
George Mason University | School of Computing | Computer Science Department
9