Docsity

CS 110 Object-Oriented Programming in Java Lab Assignment Answers | 100 Questions, Exams of Computer Science

Toggle sidebar
bg1
CS 110 Java OOP — Original Practice & Answer Guide Page 1
CS 110
Object-Oriented Programming in Java
Original Lab Practice Questions & Detailed Answers
100 Original Multiple-Choice Questions • 4 Choices Each • Explanations • Code Tracing •
OOP Practice
Format Professional practice / study guide
Question count 100
Answer choices 4 per question; globally checked for duplicate choice text
Scope Java fundamentals through object-oriented design, debugging, exceptions, polymorphism and
testing
Important scope note: Public web evidence shows that “CS 110” is used for different courses at different institutions. This guide therefore does
not claim to reproduce an official university assignment answer key. It is an independently written practice resource aligned to verified Java/OOP
learning themes.
Prepared as an original educational resource
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download CS 110 Object-Oriented Programming in Java Lab Assignment Answers | 100 Questions and more Exams Computer Science in PDF only on Docsity!

CS 110

Object-Oriented Programming in Java

Original Lab Practice Questions & Detailed Answers

100 Original Multiple-Choice Questions • 4 Choices Each • Explanations • Code Tracing •

OOP Practice

Format Professional practice / study guide

Question count 100

Answer choices 4 per question; globally checked for duplicate choice text

Scope Java fundamentals through object-oriented design, debugging, exceptions, polymorphism and testing

Important scope note: Public web evidence shows that “CS 110” is used for different courses at different institutions. This guide therefore does not claim to reproduce an official university assignment answer key. It is an independently written practice resource aligned to verified Java/OOP learning themes.

Prepared as an original educational resource

1. Research & Scope Note

The requested title combines a CS 110 course code with Java/OOP terminology, but course codes are institution-specific. Research found multiple materially different CS 110 courses. NUST currently lists CS-110 as Fundamentals of Computer Programming and CS-212 as Object Oriented Programming; Swansea describes CS-110 Programming 1 as a Java module; and Northern Arizona University uses CS 110 for an introductory course spanning OOP, Java, Python and Alice. Accordingly, this publication avoids invented university-specific assignment numbers, deadlines, or claims of official answers.

Research basis

National University of Sciences and Technology (NUST), CS-110 Fundamentals of Computer Programming Official NUST course-content PDF; identifies CS-110 as a 3-1 introductory programming course and describes C++-based fundamentals. https://nust.edu.pk/wp-content/uploads/course_content_files/870611388_CS-110%20Fundamentals%20of%20Computer%20Programming.pdf

NUST BS Computer Science study plan Official NUST curriculum page; places CS-110 Fundamentals of Computer Programming before CS-212 Object Oriented Programming. https://nbc.nust.edu.pk/department-page/cs-tentative-study-plan/

NUST CS-212 Object Oriented Programming course outline Official NUST course outline; states that Java is the major language used for OOP and covers classes, inheritance, encapsulation, polymorphism, abstraction, static members and object construction. https://nust.edu.pk/wp-content/uploads/course_content_files/525588090_CS%20212%20Course_ouline%20-%20Aimal%20Rextin.pdf

Swansea University, CS-110 Programming 1 Official university course description covering Java basics, arrays, ArrayLists, objects, classes, methods, loops and laboratory work. https://www.swansea.ac.uk/staff/n.a.harman/

Northern Arizona University, CS 110 Course Syllabus Official syllabus example demonstrating that CS 110 is institution-specific and may combine introductory programming, OOP, Java and other languages. https://www.cefns.nau.edu/~pek7/cs110/CS%20110%20Course%20Syllabus.html

How to use this guide

Use the questions as lab preparation, self-testing, code-reading practice, or review. For code-tracing items, work through state changes before checking the answer. For design questions, identify the underlying relationship—encapsulation, inheritance, interface contract, polymorphism, or composition—rather than memorizing terminology.

Java Foundations

Focus: Java Foundations. Questions progress from direct concept recognition toward application and code reasoning.

Question 1 Beginner

Which statement best describes the role of the Java compiler when a source file is compiled?

A. It stores every variable in a permanent database for later executions. B. It automatically rewrites classes into interfaces before compilation. C. It converts bytecode directly into machine instructions before a program starts. D. It translates Java source into bytecode that the JVM can execute.

Correct Answer: D. It translates Java source into bytecode that the JVM can execute.

Explanation: The Java compiler translates source code into JVM bytecode, normally stored in a .class file. The JVM then loads and executes that bytecode. Compilation and runtime execution are distinct stages.

Key Takeaway: Separate compilation from execution: javac produces bytecode; the JVM runs it.

Question 2 Beginner

Which method signature is the conventional entry point for a standalone Java application?

A. public void start(String[] args) B. private final int main(String[] args) C. static protected String main(int[] args) D. public static void main(String[] args)

Correct Answer: D. public static void main(String[] args)

Explanation: The conventional entry point is public static void main(String[] args). public makes it accessible to the launcher, static allows invocation without constructing an object, void specifies no return value, and String[] receives command-line arguments.

Key Takeaway: Know the exact role of each keyword in the Java entry point.

Question 3 Beginner

A file contains a public class named InventoryReport. What filename is required by Java's normal source-file naming

rule?

A. InventoryReport.txt B. Inventory_Report.class C. InventoryReport.java D. ReportInventory.java

Correct Answer: C. InventoryReport.java

Explanation: A public top-level class normally must have a source filename matching the class name, followed by .java. The compiled bytecode is a separate artifact.

Key Takeaway: Public top-level class name and source filename should match.

Question 4 Beginner

Which statement about Java comments is correct?

A. \ starts a comment and automatically comments the rest of the file.

B. is the ordinary syntax for all Java comments. C. // starts a single-line comment; /* ... */ can span lines. D. # starts a standard Java comment that lasts until the next statement.

Correct Answer: C. // starts a single-line comment; /* ... */ can span lines.

Explanation: Java supports // for single-line comments and /* ... */ for block comments. The # and HTML-style comment markers are not Java comment syntax.

Key Takeaway: Use Java's own comment delimiters rather than borrowing syntax from another language.

Variables & Data Types

Focus: Variables & Data Types. Questions progress from direct concept recognition toward application and code reasoning.

Question 5 Beginner

Which declaration creates an integer variable named seats with the value 18?

A. Int seats <- 18; B. integer seats := 18; C. number seats = 18; D. int seats = 18;

Correct Answer: D. int seats = 18;

Explanation: int is Java's primitive integer type, and assignment uses =. The other declarations use syntax or type names that are not valid Java.

Key Takeaway: Java primitive declarations use a type, variable name, and optional initialization.

Question 6 Beginner

Which type is the most direct choice for storing true/false state in Java?

A. bit B. boolean C. truthValue D. logical

Correct Answer: B. boolean

Explanation: Java's primitive boolean type represents true or false values. The other names are not Java primitive types.

Key Takeaway: Use boolean for two-state logical conditions.

Question 7 Beginner

What is the value of total after this statement executes? int total = 7 + 5;

A. 2
B. 75
C. 35
D. 12

Correct Answer: D. 12

Explanation: The plus operator adds integer operands, so 7 + 5 evaluates to 12. The digits are not concatenated because both operands are numeric ints.

B. 5
C. 13
D. 36

Correct Answer: C. 13

Explanation: The compound assignment x += 9 is equivalent to x = x + 9, so 4 becomes 13.

Key Takeaway: Compound assignment updates the existing value using the operator on the left.

Question 12 Beginner

Which operator checks whether two primitive numeric values are equal?

A. equals B. == C. = D. <>

Correct Answer: B. ==

Explanation: For primitive values, == compares values. A single = performs assignment; equals is a method used by objects; <> is not Java's equality operator.

Key Takeaway: Distinguish assignment from comparison.

Conditionals

Focus: Conditionals. Questions progress from direct concept recognition toward application and code reasoning.

Question 13 Beginner

Given int temp = 31;, which branch executes in if (temp &gt; 30) { A } else { B }?

A. Both branches execute once. B. Neither branch can execute because 31 is not an integer literal. C. The B branch executes. D. The A branch executes.

Correct Answer: D. The A branch executes.

Explanation: 31 is greater than 30, so the condition is true and the first branch runs. An if/else chooses one branch based on the condition.

Key Takeaway: Evaluate the condition before deciding which branch runs.

Question 14 Beginner

Which structure is most suitable when exactly one of several discrete menu codes should select an action?

A. try B. while C. import D. switch

Correct Answer: D. switch

Explanation: switch is designed for selecting among discrete cases. while repeats based on a condition, try handles exceptions, and import makes types available.

Key Takeaway: Use switch when one expression is matched against discrete cases.

Question 15 Beginner

What does an else if branch provide in a conditional chain?

A. Another condition to test only if earlier conditions were false. B. A second method declaration inside the first branch. C. A loop that repeats the previous condition indefinitely. D. A way to force all earlier branches to execute.

Correct Answer: A. Another condition to test only if earlier conditions were false.

Explanation: An else if adds another condition that is considered only when the preceding if/else-if conditions have not matched.

Key Takeaway: Conditional chains are evaluated from top to bottom.

Question 16 Beginner

Which condition correctly tests whether n is outside the inclusive interval 10 through 20?

A. n > 10 && n < 20 B. n == 10 || n == 20 C. n <= 10 && n >= 20 D. n < 10 || n > 20

Correct Answer: D. n < 10 || n > 20

Explanation: A value outside the interval is either below 10 or above 20, so || connects the two possibilities.

Key Takeaway: Think in terms of the two disjoint ways a value can be outside a range.

Loops

Focus: Loops. Questions progress from direct concept recognition toward application and code reasoning.

Question 17 Beginner

How many times does the body execute? for (int i = 0; i &lt; 4; i++) { ... }

A. 0 [Q17-1]
B. 3 [Q17-2]
C. 4 [Q17-3]
D. 5 [Q17-4]

Correct Answer: C. 4 [Q17-3]

Explanation: The loop starts at 0 and runs for i = 0, 1, 2, and 3. When i becomes 4, the condition i < 4 is false.

Key Takeaway: A zero-based loop with i < 4 visits four values.

Question 18 Beginner

Which loop is guaranteed to execute its body at least once?

A. enhanced if B. for C. while [Q18-3]

Methods

Focus: Methods. Questions progress from direct concept recognition toward application and code reasoning.

Question 21 Beginner

Which method declaration returns an int and accepts one int parameter?

A. static int doubleValue(int n) B. static void doubleValue(int n) C. int doubleValue() D. static integer doubleValue(number n)

Correct Answer: A. static int doubleValue(int n)

Explanation: The first declaration specifies int as the return type and one int parameter. void means no return value, and the other forms use incompatible syntax or omit the parameter.

Key Takeaway: A method signature's return type and parameter list describe what it produces and accepts.

Question 22 Beginner

What does a return statement do in a non-void Java method?

A. It creates a new object without executing the constructor. B. It supplies a value to the caller and ends that method invocation. C. It changes a local variable into a class field. D. It repeats the method from its first line automatically.

Correct Answer: B. It supplies a value to the caller and ends that method invocation.

Explanation: In a non-void method, return supplies a value compatible with the declared return type and ends the current invocation.

Key Takeaway: return transfers a result back to the caller.

Question 23 Beginner

If a method is declared void printBanner(), which statement is valid inside it?

A. return 12; B. return; C. return false; D. return "done";

Correct Answer: B. return;

Explanation: A void method may use a bare return to exit early, but it cannot return a value.

Key Takeaway: void means the method does not return a value.

Question 24 Beginner

What is true about a local variable declared inside a method?

A. Its scope is limited to the relevant block in which it is declared. B. It automatically becomes a field shared by every object of the class. C. It remains available after the method invocation through the object.

D. It can be referenced from every class in the package.

Correct Answer: A. Its scope is limited to the relevant block in which it is declared.

Explanation: A local variable belongs to its declaring method/block scope. It is not automatically a field or globally accessible state.

Key Takeaway: Scope controls where a name can be referenced.

Arrays

Focus: Arrays. Questions progress from direct concept recognition toward application and code reasoning.

Question 25 Intermediate

For int[] marks = new int[6];, what is the index of the final element?

A. 7
B. 6
C. 5 [Q25-3]
D. 4 [Q25-4]

Correct Answer: C. 5 [Q25-3]

Explanation: Java arrays are zero-indexed. An array of length 6 has valid indices 0 through 5.

Key Takeaway: The last index is length minus one.

Question 26 Intermediate

What value is stored in every element of a newly created int array before assignments change them?

A. 0 [Q26-1]

B. undefined C. - D. null

Correct Answer: A. 0 [Q26-1]

Explanation: Java initializes int array elements to 0. null applies to reference types, not primitive int elements.

Key Takeaway: Java arrays receive default values when created.

Question 27 Intermediate

Which enhanced-for declaration correctly visits each element of an int array named values?

A. for (int value : values) B. for (value int : values) C. foreach (int value in values) D. repeat (int value from values)

Correct Answer: A. for (int value : values)

Explanation: The enhanced-for syntax is for (type variable : arrayOrIterable). The first option follows Java's syntax.

Key Takeaway: Enhanced-for uses a colon between the element variable and the array/iterable.

Question 28 Intermediate

What exception is most directly associated with accessing scores[8] when scores has length 8?

Explanation: substring(begin, end) includes the begin index and excludes the end index, so indices 2, 3, and 4 are returned.

Key Takeaway: Java substring uses an inclusive start and exclusive end.

Question 32 Intermediate

What is a key property of Java String objects?

A. String concatenation can never create a new String object. B. Their contents are immutable after a String object has been created. C. Calling toUpperCase() changes the original String object in place. D. Every String variable always refers to a mutable character array.

Correct Answer: B. Their contents are immutable after a String object has been created.

Explanation: String is immutable: operations that appear to change text produce a new String value rather than modifying the original object.

Key Takeaway: Treat String operations as producing new values.

ArrayList

Focus: ArrayList. Questions progress from direct concept recognition toward application and code reasoning.

Question 33 Intermediate

Which declaration creates an ArrayList intended to hold String elements?

A. ArrayList names = new ArrayList<>(); B. ArrayList names = new String<>(); C. List names = new ArrayList(String); D. ArrayList names = ArrayList();

Correct Answer: A. ArrayList names = new ArrayList<>();

Explanation: The first declaration uses a generic type argument String and constructs an ArrayList with the diamond operator.

Key Takeaway: Generics express the element type stored in an ArrayList.

Question 34 Intermediate

Which method adds an element to the end of an ArrayList named tasks?

A. tasks.insertEnd("backup") B. tasks.pushTail("backup") C. tasks.appendLast("backup") D. tasks.add("backup")

Correct Answer: D. tasks.add("backup")

Explanation: ArrayList.add(element) appends the element to the end. The other method names are not standard ArrayList methods.

Key Takeaway: Use add(element) for ordinary append behavior.

Question 35 Intermediate

If an ArrayList named codes contains three elements, what does codes.size() return?

A. 3 [Q35-1]

B. The last index, which is 2 C. 4 [Q35-3] D. 2 [Q35-4]

Correct Answer: A. 3 [Q35-1]

Explanation: size() reports the number of elements, not the last index. With three elements, size is 3 and the last index is 2.

Key Takeaway: Collection size counts elements; indexes begin at zero.

Question 36 Intermediate

Which operation removes the element at index 2 from an ArrayList named queue?

A. queue.drop(2) B. queue.eraseIndex(2) C. queue.remove(2) D. queue.deleteAt(2)

Correct Answer: C. queue.remove(2)

Explanation: ArrayList.remove(int index) removes the element at the specified index. The other names are not standard ArrayList methods.

Key Takeaway: ArrayList removal can be performed by index with remove(int).

Classes & Objects

Focus: Classes & Objects. Questions progress from direct concept recognition toward application and code reasoning.

Question 37 Intermediate

What does a Java class primarily define?

A. A blueprint describing state and behavior that objects can have. B. A compiler instruction that replaces every method call. C. A single object that can never have another instance. D. A package containing only primitive variables.

Correct Answer: A. A blueprint describing state and behavior that objects can have.

Explanation: A class defines fields, constructors, and methods that describe the state and behavior of its instances. Multiple objects can be created from one class.

Key Takeaway: Think of a class as a type definition and an object as an instance.

Question 38 Intermediate

Which statement creates an object from a class named Sensor?

A. Sensor s = Sensor.createType; B. Sensor s = new Sensor(); C. new Sensor = s; D. object Sensor s();

Correct Answer: B. Sensor s = new Sensor();

Constructors

Focus: Constructors. Questions progress from direct concept recognition toward application and code reasoning.

Question 41 Intermediate

Which feature identifies a constructor in Java?

A. It has the same name as its class and has no declared return type. B. It must be static so it can run before object creation. C. It is declared with the keyword constructor before the class name. D. It must always be named initialize and return the object reference.

Correct Answer: A. It has the same name as its class and has no declared return type.

Explanation: A constructor has the same name as the class and does not declare a return type. It is invoked when an object is created.

Key Takeaway: Constructors initialize newly created objects.

Question 42 Intermediate

What happens if a class declares no constructor at all?

A. The compiler converts every instance method into a constructor. B. The class can never be instantiated under any circumstances. C. Java can provide a default no-argument constructor, subject to the class's accessibility. D. Java automatically creates a constructor for every possible parameter list.

Correct Answer: C. Java can provide a default no-argument constructor, subject to the class's accessibility.

Explanation: If no constructor is explicitly declared, Java can supply a default no-argument constructor with the same accessibility as the class. Once any constructor is declared, that automatic constructor is not supplied.

Key Takeaway: Declaring a constructor changes whether the compiler supplies the default one.

Question 43 Intermediate

Why might a class provide two constructors with different parameter lists?

A. To offer multiple initialization choices for different object-creation needs. B. To allow constructors to have unrelated class names. C. To convert instance methods into static utilities. D. To make every constructor execute simultaneously for one object.

Correct Answer: A. To offer multiple initialization choices for different object-creation needs.

Explanation: Multiple constructors with different parameter lists provide overloaded initialization paths.

Key Takeaway: Constructor overloading gives callers different ways to initialize an object.

Question 44 Intermediate

Inside a constructor, what is a common purpose of this.name = name; when both names exist?

A. It assigns the parameter named name to the current object's field named name. B. It creates a second object whose class is named name. C. It converts the field into a static variable.

D. It calls a superclass constructor named name.

Correct Answer: A. It assigns the parameter named name to the current object's field named name.

Explanation: this.name refers to the current object's field, while the unqualified name refers to the constructor parameter. The statement resolves the naming collision and assigns the parameter to the field.

Key Takeaway: this distinguishes an instance field from a parameter with the same name.

Encapsulation

Focus: Encapsulation. Questions progress from direct concept recognition toward application and code reasoning.

Question 45 Intermediate

Which design most directly supports encapsulation for a balance field?

A. Store the balance only in a local variable inside main. B. Declare the field final even when it must change frequently. C. Make the field public so every class can modify it freely. D. Keep the field private and expose controlled methods for reading or changing it.

Correct Answer: D. Keep the field private and expose controlled methods for reading or changing it.

Explanation: Encapsulation hides internal state and controls access through a public interface. A private field plus carefully designed methods is a common Java implementation.

Key Takeaway: Hide representation details and expose controlled behavior.

Question 46 Intermediate

Why is a private field inaccessible through direct access from unrelated classes?

A. private means the field exists only while a constructor executes. B. private automatically converts the field into a constant. C. private prevents the JVM from allocating storage for the field. D. The private access modifier restricts direct member access to the declaring class.

Correct Answer: D. The private access modifier restricts direct member access to the declaring class.

Explanation: private limits direct access to the declaring class. Other classes must use permitted methods or other public/protected interfaces.

Key Takeaway: Access modifiers control who can directly use a member.

Question 47 Intermediate

What is a useful reason for validating input in a setter method?

A. The class can reject or transform invalid state before storing it. B. Validation eliminates the need for constructors in every class. C. A setter automatically makes the field public to every caller. D. A setter prevents all runtime exceptions in the program.

Correct Answer: A. The class can reject or transform invalid state before storing it.

Explanation: A setter can enforce invariants such as nonnegative quantities before updating the object's internal state.

Key Takeaway: Controlled mutation lets a class protect its invariants.

C. The class definition itself is modified. D. The JVM automatically creates a third object to hold the change.

Correct Answer: B. The same object's field is changed, so observing it through a can show the new value.

Explanation: If a == b, both references identify one object. A field mutation through either reference changes that shared instance.

Key Takeaway: Multiple references can point to one object.

Question 52 Intermediate

What does a null reference indicate?

A. It refers to the class's static area only. B. It does not currently refer to an object instance. C. It refers to an object whose fields all contain zero. D. It is an object whose methods have been disabled.

Correct Answer: B. It does not currently refer to an object instance.

Explanation: null is the absence of an object reference. Dereferencing a null reference can cause NullPointerException.

Key Takeaway: null means no object is currently referenced.

Static Members

Focus: Static Members. Questions progress from direct concept recognition toward application and code reasoning.

Question 53 Intermediate

What does a static field represent in a typical class?

A. One class-level variable shared by the class's instances rather than a separate copy per object. B. A field that can only be accessed from constructors. C. A field that must contain a String literal. D. A local variable recreated every time an instance method runs.

Correct Answer: A. One class-level variable shared by the class's instances rather than a separate copy per object.

Explanation: A static field belongs to the class rather than to each individual instance, so there is one class-level storage location for the field.

Key Takeaway: static members belong to the class rather than a particular instance.

Question 54 Intermediate

Which call is appropriate for a static method MathBox.square(int x)?

A. 7.MathBox.square() B. new MathBox.square(7) C. MathBox().square(7) D. MathBox.square(7)

Correct Answer: D. MathBox.square(7)

Explanation: A static method can be invoked through the class name, so MathBox.square(7) is the appropriate form.

Key Takeaway: Use the class name for ordinary static method calls.

Question 55 Intermediate

Why can't a static method directly use an instance field named count without an object reference?

A. Static methods execute only during compilation and therefore cannot read memory. B. Instance fields exist only inside constructors. C. A static method has no implicit current-instance reference. D. Java forbids static methods from reading any variables.

Correct Answer: C. A static method has no implicit current-instance reference.

Explanation: A static method is associated with the class and does not have an implicit this reference, so it cannot directly identify which object's instance field should be used.

Key Takeaway: Instance state requires a particular object.

Question 56 Intermediate

What is a common use of a static final field such as MAX_RETRIES?

A. Creating a new mutable value for every object. B. Representing a class-level constant whose value should not be reassigned. C. Allowing subclasses to override the constant at runtime. D. Storing temporary method-local input.

Correct Answer: B. Representing a class-level constant whose value should not be reassigned.

Explanation: A static final field is commonly used for a constant shared at class level. final prevents reassignment of the field after initialization.

Key Takeaway: static final is a common pattern for named constants.

Inheritance

Focus: Inheritance. Questions progress from direct concept recognition toward application and code reasoning.

Question 57 Intermediate

What does class Sedan extends Vehicle indicate?

A. Sedan can inherit from any number of unrelated classes. B. Sedan is a subclass that inherits accessible members and behavior from Vehicle. C. Sedan and Vehicle become unrelated classes with shared source text. D. Vehicle is automatically converted into an interface named Sedan.

Correct Answer: B. Sedan is a subclass that inherits accessible members and behavior from Vehicle.

Explanation: extends establishes a superclass/subclass relationship. A Sedan object is a Vehicle as well, subject to Java's inheritance rules.

Key Takeaway: extends creates a class inheritance relationship.

Question 58 Intermediate

Which keyword is used to invoke a superclass constructor from a subclass constructor?

A. base B. up