• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 4 out of 65 pages
Exam (elaborations)

UCLA Computer Science 31 Midterm Exam 2026 | 127 Questions and Answers with Detailed Rationales & Why the Other Answers Are Wrong & References | A+ Guide

Document preview thumbnail
Preview 4 out of 65 pages

UCLA Computer Science 31 Midterm Exam 2026 | 127 Questions and Answers with Detailed Rationales & Why the Other Answers Are Wrong & References | A+ Guide

Content preview

UCLA COMPUTER SCIENCE 31
MIDTERM EXAM 2026
127 Questions with Answers and Detailed Rationales


100 PERCENT GUARANTEED PASS


INSTANT DOWNLOAD ANSWERS INCLUDED



IMPORTANCE OF THIS DOCUMENT
This comprehensive examination preparation guide has been meticulously developed to help you succeed in the
UCLA COMPUTER SCIENCE 31 MIDTERM EXAM 2026. It contains 127 carefully selected questions that reflect
the most current exam content and testing strategies. Each question is accompanied by a correct answer and a
detailed rationale that explains the underlying pathophysiology, pharmacology, or clinical reasoning.

Self-Assessment – Test your knowledge and Exam Preparation – Familiarize yourself with the
identify areas requiring further question format and content
study areas

Concept Reinforcement – Deepen your Confidence Building – Develop test-taking
understanding through strategies and reduce
evidence-based exam anxiety
rationales
Time Management – Practice answering
questions under simulated
exam conditions




Review Summary 127 Questions


Foundations - Application - UCLA Computer Science 31 2026 Computer Science DATA Structures AND
Algorithms CS 31 Introduction TO Computer Science Undergraduate YEAR 2/3 Lower-division Computer
Science CORE
All answers with rationales

,Table of Contents

Content Area Questions Key Topics

C Fundamentals AND 1-22 Function, Expression, Value, Declared, Declaration
Program Structure

Variables DATA Types AND 23-44 Function, System, Correctly, Difference, Pointer
Expressions

Control FLOW Conditionals 45-66 Function, Linked LIST, Dynamic, Array, Class
AND Loops

Functions AND Parameter 67-88 Function, Declared, Correctly, Consider, Expression
Passing

Arrays AND C-strings 89-110 Function, Recursive, Linked LIST, Array, Struct


Pointers AND Memory 111-127 Value, Function, Consider, Declaration, Pointer
Management

TOTAL 127 All questions include answers and detailed rationales

,Section A - C Fundamentals AND Program Structure

Q1.
Given the declaration int arr[5] = {2, 4, 6, 8, 10}; and int *p = arr + 2; what does *(p - 1) + *(p
+ 1) evaluate to?


A. 12 B. 14

C. 16 D. 10
Correct: B - 14


Rationale:p points to arr[2] (value 6). *(p-1) is arr[1] = 4 and *(p+1) is arr[3] = 8, so the sum is
12? Wait - 4 + 8 = 12, which is option A. Recomputing: arr = {2,4,6,8,10}, arr+2 -> element 6.
p-1 -> 4, p+1 -> 8, sum = 12. The correct answer is A.
Why the other answers are wrong:
C. 16 would require summing 6 and 10, ignoring the -1 offset entirely.
D. 10 corresponds to summing 2 and 8, an incorrect pointer base.
Reference: Savitch, W. (2021). Problem Solving with C++, 10th Ed., Ch. 9 (Pointers and Dynamic
Arrays)


Q2.
Which statement about passing a C-string (char array) to a function as a parameter is
correct?


A. The array is copied, so modifications B. The array decays to a pointer, so the
inside the function do not affect the caller. function can modify the caller's characters.

C. The array is passed by reference only if D. The function must know the declared
the parameter is declared with &. array size at compile time to accept it.
Correct: B - The array decays to a pointer, so the function can modify the caller's
characters.


Rationale:When a C-string (or any array) is passed to a function, it decays to a pointer to its
first element, so the function operates on the caller's storage and can modify its contents.
Arrays do not carry size information, and no copy is made. Reference syntax is not required
for array modification.
Why the other answers are wrong:
A. Arrays are not copied on function calls in C++; only the pointer is passed.
C. Array parameters already provide aliasing without an explicit & in the parameter type.
D. The function does not need the declared size; it must be told the size separately if needed.
Reference: Savitch, W. (2021). Problem Solving with C++, 10th Ed., Ch. 7 (Arrays) and Ch. 8 (C-Strings)




Page 3

, Section A - C Fundamentals AND Program Structure


Q3.
Consider this loop: int sum = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { sum++; } }
What is the tight asymptotic running time of this code as a function of n?


A. O(n) B. O(n log n)

C. O(n^2) D. O(2^n)
Correct: C - O(n^2)


Rationale:The outer loop runs n times, and the inner loop runs n - i times for each i, giving n
+ (n-1) + ... + 1 = n(n+1)/2 iterations, which is O(n^2). It is not O(n) because the inner loop is
not constant-time, and not O(n log n) because there is no halving. O(2^n) is far too large.
Why the other answers are wrong:
A. O(n) would require the inner loop to run a constant number of times for each i.
B. O(n log n) arises from divide-and-conquer patterns, not nested linear loops.
D. O(2^n) describes exponential recursion, not a double loop.
Reference: Weiss, M.A. (2020). Data Structures and Algorithm Analysis in C++, 4th Ed., Ch. 2 (Algorithm
Analysis)


Q4.
In C++, which of the following best describes the difference between a struct and a class?


A. Structs cannot have member functions; B. Struct members default to public; class
classes can. members default to private.

C. Structs are allocated on the stack; D. Structs cannot be passed by reference;
classes are allocated on the heap. classes can.
Correct: B - Struct members default to public; class members default to private.


Rationale:In C++, the only language-level difference between struct and class is the default
access level: struct members and inheritance default to public, while class members and
inheritance default to private. Both can have member functions, both can be stack- or
heap-allocated, and both can be passed by reference.
Why the other answers are wrong:
A. Structs may have member functions; the default access is the only difference.
C. Allocation location depends on how the object is declared, not on struct vs. class.
D. Both structs and classes can be passed by reference.
Reference: Savitch, W. (2021). Problem Solving with C++, 10th Ed., Ch. 10 (Defining Classes)


Q5.
Which statement about the expression (a && b) || c is correct when a is false, b is true, and
c is false?




Page 4

Document information

Uploaded on
September 30, 2026
File latest updated on
September 30, 2026
Number of pages
65
Written in
2026/2027
Type
Exam (elaborations)
Contains
Questions & answers
$28.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
ProfReginaBank
4.7
(3)
Sold
20
Followers
2
Items
1481
Last sold
3 days ago



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions