Introduction to C Programming
- 77Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
Which of the following describes the purpose of Scratch in the context of CS50 week one?
Which of the following describes the purpose of Scratch in the context of CS50 week one?
77 questions
- 1
Which of the following describes the purpose of Scratch in the context of CS50 week one?
- A
To elucidate fundamental programming concepts in an accessible way.
- B
To prepare students exclusively for game development careers.
- C
To teach advanced graphical interface design and animations.
- D
To primarily introduce complex textual syntax to beginners directly.
Correct answer
ATo elucidate fundamental programming concepts in an accessible way.
Explanation
Scratch's primary goal in CS50 is to make programming concepts accessible and user-friendly, allowing students to grasp fundamental ideas like functions, conditionals, loops, and variables before moving to more complex textual languages like C.
- A
- 2
What is the C language equivalent of Scratch's 'when green flag clicked' block?
- A
The curly braces that enclose the main program logic.
- B
The '#include <stdio.h>' directive at the top of the file.
- C
The 'printf' function that displays output on the screen.
- D
The 'int main(void)' function that defines the program's entry point.
Correct answer
DThe 'int main(void)' function that defines the program's entry point.
Explanation
The 'int main(void)' function in C serves a similar purpose to Scratch's 'when green flag clicked' block, indicating the starting point or entry point of the program's execution.
- A
- 3
What does 'source code' represent in the context of programming?
- A
Machine instructions generated by a computer's hardware.
- B
The graphical interface used to interact with a program.
- C
A human-readable version of code written by programmers.
- D
The binary zeros and ones that computers directly understand.
Correct answer
CA human-readable version of code written by programmers.
Explanation
Source code is the human-readable text written by programmers, which uses a specific language syntax (like C) but is not directly understood by computers. It needs to be translated into machine code.
- A
- 4
Why can't computers directly understand 'source code'?
- A
Source code contains too many complex English-like words.
- B
Source code is designed for network communication, not processing.
- C
Computers only understand graphical user interfaces and icons.
- D
Computers exclusively operate on a system of zeros and ones.
Correct answer
DComputers exclusively operate on a system of zeros and ones.
Explanation
Computers fundamentally operate using a binary system, meaning they only understand patterns of zeros and ones (machine code). Source code, being human-readable, must be translated into this binary format for the computer to execute it.
- A
- 5
What is the primary role of a 'compiler' in the programming process?
- A
To write the source code based on a set of problem specifications.
- B
To translate source code into machine code (zeros and ones).
- C
To interpret human language commands into programming syntax.
- D
To execute the machine code and display results on screen.
Correct answer
BTo translate source code into machine code (zeros and ones).
Explanation
A compiler is a special program whose purpose in life is to translate source code, written in a human-readable language like C, into machine code, which consists of the zeros and ones that the computer's processor can directly understand and execute.
- A
- 6
Which industry-standard tool is used for CS50 problem sets and is available at cs50.dev?
- A
Adobe Photoshop
- B
Microsoft Word
- C
Visual Studio Code (VS Code)
- D
Google Chrome
Correct answer
CVisual Studio Code (VS Code)
Explanation
The programming environment used for CS50 problem sets is Visual Studio Code (VS Code), specifically a cloud-based version accessible at cs50.dev, which is a popular industry-standard tool.
- A
- 7
What is the function of the 'colorization' feature in VS Code?
- A
It converts the code into colorful graphical representations.
- B
It indicates errors by turning all incorrect code red for the user.
- C
It automatically highlights different parts of the code to draw attention.
- D
It allows users to manually change the color of their code elements.
Correct answer
CIt automatically highlights different parts of the code to draw attention.
Explanation
VS Code automatically colorizes code, highlighting different parts (like keywords, strings, and comments) in various colors. This feature is meant to be illuminating, helping programmers to visually distinguish different elements and ideas within their code.
- A
- 8
What is a 'terminal window' primarily used for in VS Code?
- A
To write and edit source code in a visually rich editor.
- B
To view a visual representation of all open programming tabs.
- C
To use a command-line interface by typing commands with a keyboard.
- D
To graphically drag and drop files and folders with a mouse.
Correct answer
CTo use a command-line interface by typing commands with a keyboard.
Explanation
The terminal window in VS Code provides a command-line interface (CLI), allowing programmers to interact with the system by typing textual commands using their keyboard, which is often more efficient than using a mouse.
- A
- 9
What is the purpose of the 'File Explorer' in VS Code?
- A
To visually manage and interact with files and folders.
- B
To provide a chat interface for asking programming questions.
- C
To display the output of a running program in real time.
- D
To allow users to select different programming languages.
Correct answer
ATo visually manage and interact with files and folders.
Explanation
The File Explorer in VS Code offers a graphical user interface (GUI) that allows users to visually see, manipulate, and organize their files and folders, similar to a file manager on a Mac or PC.
- A
- 10
What does 'GUI' stand for, and what does it typically involve?
- A
Graphical User Interface; buttons, icons, and menus for interaction.
- B
General User Interface; focus on keyboard shortcuts only.
- C
Graphical User Instruction; text-based command sequences.
- D
Global Utility Integration; a system for managing cloud resources.
Correct answer
AGraphical User Interface; buttons, icons, and menus for interaction.
Explanation
GUI stands for Graphical User Interface, which is characterized by graphical elements like buttons, icons, and menus, allowing users to interact with a system using visual aids rather than text commands.
- A
- 11
How is a 'command line interface' (CLI) different from a 'graphical user interface' (GUI)?
- A
CLI relies on clicking icons, while GUI relies on typing text.
- B
CLI requires an internet connection, while GUI works offline.
- C
CLI is visually advanced, while GUI is text-only and basic.
- D
CLI uses typed commands, while GUI uses buttons, icons, and menus.
Correct answer
DCLI uses typed commands, while GUI uses buttons, icons, and menus.
Explanation
A Command Line Interface (CLI) involves typing commands to interact with a computer system, whereas a Graphical User Interface (GUI) uses visual elements such as buttons, icons, and menus that users can click or tap on.
- A
- 12
What is the conventional file extension for C source code files?
- A
.exe
- B
.txt
- C
.html
- D
.c
Correct answer
D.c
Explanation
By convention, when writing code in C, you literally name the file something.c, with '.c' being the standard file extension for C source code.
- A
- 13
Which of the following commands is used to create a new file named 'hello.c' in the CS50 VS Code environment?
- A
open hello.c
- B
make hello.c
- C
run hello.c
- D
code hello.c
Correct answer
Dcode hello.c
Explanation
The command 'code hello.c' is used in the CS50 VS Code environment to create a new file named 'hello.c' and open it in the editor tab.
- A
- 14
What does the 'make hello' command accomplish in the C programming workflow?
- A
It translates the 'hello.c' source code into machine code.
- B
It creates a new source code file named 'hello'.
- C
It executes the program named 'hello' directly.
- D
It deletes the compiled 'hello' program from the directory.
Correct answer
AIt translates the 'hello.c' source code into machine code.
Explanation
The 'make hello' command (or more accurately, 'make' triggering the compiler) is responsible for converting the human-readable 'hello.c' source code into an executable 'hello' file containing machine code that the computer can understand and run.
- A
- 15
What is the purpose of the './hello' command after compilation?
- A
To open the 'hello.c' source code in a text editor.
- B
To delete both the 'hello.c' and 'hello' files from the system.
- C
To run the compiled machine code program named 'hello'.
- D
To recompile the 'hello.c' file if changes were made.
Correct answer
CTo run the compiled machine code program named 'hello'.
Explanation
After successfully compiling the source code into machine code, the './hello' command is used to execute or run the compiled program named 'hello'. The './' indicates that the program is located in the current directory.
- A
- 16
What does the dollar sign '$' typically signify at the beginning of a terminal prompt?
- A
It denotes the end of a previous command's output or execution.
- B
It signifies that the user has administrative or root privileges.
- C
It indicates that the user is currently dealing with currency calculations.
- D
It is a convention indicating where to type commands in the terminal.
Correct answer
DIt is a convention indicating where to type commands in the terminal.
Explanation
The dollar sign '$' at the beginning of a terminal prompt is a common convention that simply indicates that it is where the user should type their commands. It has no relation to currency.
- A
- 17
What is the difference between 'hello.c' and 'hello' (without the .c extension) after successful compilation?
- A
hello.c is a temporary file, hello is the permanent program file.
- B
hello.c is the source code written by a human, hello is the compiled machine code.
- C
hello.c contains executable instructions, hello contains program metadata.
- D
hello.c is the program's configuration file, hello is the data file.
Correct answer
Bhello.c is the source code written by a human, hello is the compiled machine code.
Explanation
'hello.c' is the source code file written by the programmer, containing human-readable C language instructions. 'hello' (without the extension) is the executable file created by the compiler, containing the machine code (zeros and ones) that the computer can directly run.
- A
- 18
If you simply type 'hello' at the terminal prompt instead of './hello' after compilation, what is the likely outcome?
- A
The program will run, but with reduced execution speed.
- B
The system will open the 'hello.c' source code in the editor.
- C
The compiler will automatically recompile the 'hello.c' file.
- D
The terminal will report 'command not found' because the location is missing.
Correct answer
DThe terminal will report 'command not found' because the location is missing.
Explanation
If you type 'hello' without the './' prefix, the terminal will report 'command not found'. The './' explicitly tells the computer to look for and run the program within the current directory, whereas 'hello' alone suggests a system-wide command.
- A
- 19
What is the C language equivalent of Scratch's 'say' block, which displays output on the screen?
- A
inputf
- B
displayf
- C
outputf
- D
printf
Correct answer
Dprintf
Explanation
The 'printf' function in C is the equivalent of Scratch's 'say' block. It is used to print or display text and other formatted output to the screen.
- A
- 20
What is the purpose of parentheses '()' immediately following a function name in C?
- A
To indicate that the function is being declared, not called.
- B
To mark the beginning and end of the function's output.
- C
To enclose the input arguments or parameters for the function.
- D
To denote the end of the function's definition.
Correct answer
CTo enclose the input arguments or parameters for the function.
Explanation
In C, as in many programming languages, parentheses '()' after a function name are used to enclose the input values, known as arguments or parameters, that are passed to the function.
- A
- 21
Why are double quotes '""' required around text strings passed to 'printf' in C?
- A
To distinguish them from numerical or boolean values.
- B
To make the text visible and readable on the screen.
- C
To indicate that the text should be treated as a literal string.
- D
To enable special formatting characters within the text.
Correct answer
CTo indicate that the text should be treated as a literal string.
Explanation
In C, double quotes are necessary to delineate a literal string of text. They tell the compiler that the characters within the quotes should be treated as a sequence of characters, not as variable names or other code elements.
- A
- 22
What does the escape sequence '\n' do when used within a 'printf' statement?
- A
It prevents any further text from being displayed on the screen.
- B
It moves the cursor to the beginning of the current line.
- C
It inserts a tab character into the output.
- D
It creates a new line, similar to hitting the Enter key.
Correct answer
DIt creates a new line, similar to hitting the Enter key.
Explanation
The '\n' is an escape sequence that represents a newline character. When 'printf' encounters it, the cursor moves to the beginning of the next line, effectively creating a line break in the output.
- A
- 23
What is the outcome if you omit '\n' at the end of a 'printf("Hello, world")' statement?
- A
The subsequent terminal prompt will appear on the same line as the output.
- B
The program will automatically insert a newline character for formatting.
- C
The output 'Hello, world' will not be displayed at all.
- D
The program will crash with a runtime error.
Correct answer
AThe subsequent terminal prompt will appear on the same line as the output.
Explanation
Without '\n', the 'printf' function will display 'Hello, world' but will not move the cursor to the next line. As a result, the terminal's command prompt (e.g., '$') will appear immediately after 'world' on the same line, making the output look less tidy.
- A
- 24
What is the escape sequence for printing a literal double quotation mark '"' within a string in C?
- A
//"
- B
"
- C
\q
- D
\"
Correct answer
B"
Explanation
To print a literal double quotation mark, you must 'escape' it with a backslash. So, '"' tells the compiler to treat the double quote as a character to be printed, rather than as the end of the string literal.
- A
- 25
In C, what is the meaning of the equal sign '=' when used in a statement like 'int x = 10;'?
- A
It declares 'x' as a constant integer with the value '10'.
- B
It tests for equality between 'x' and '10'.
- C
It signifies that 'x' is greater than or equal to '10'.
- D
It assigns the value '10' to the variable 'x'.
Correct answer
DIt assigns the value '10' to the variable 'x'.
Explanation
In C, a single equal sign '=' is the assignment operator. It assigns the value on its right-hand side (e.g., 10) to the variable on its left-hand side (e.g., x).
- A
- 26
What is a 'side effect' of a function, as described in the context of 'printf'?
- A
An input argument that modifies the function's internal state.
- B
A value that the function returns to the calling code.
- C
A visible or audible action that occurs on the screen or system.
- D
An unexpected error or bug that causes the program to crash.
Correct answer
CA visible or audible action that occurs on the screen or system.
Explanation
A side effect of a function, such as 'printf', is an action that happens externally, like displaying text on the screen, playing a sound, or performing a visual change. It's distinct from a return value, which is data directly handed back by the function.
- A
- 27
What is the purpose of '#include <stdio.h>' at the top of a C file?
- A
It tells the compiler to ignore all subsequent code in the file.
- B
It includes the 'standard input/output' library for functions like 'printf'.
- C
It declares a new data type named 'stdio' for string manipulation.
- D
It defines the main entry point for the program's execution.
Correct answer
BIt includes the 'standard input/output' library for functions like 'printf'.
Explanation
'#include <stdio.h>' is a preprocessor directive that includes the 'standard input/output' header file. This file contains declarations for standard I/O functions like 'printf', making them available for use in the current C program.
- A
- 28
What error occurs if '#include <stdio.h>' is forgotten when using 'printf'?
- A
The program will enter an infinite loop during execution.
- B
The program will compile but produce incorrect output.
- C
The compiler will report 'syntax error: missing semicolon'.
- D
The compiler will report 'call to undeclared library function printf'.
Correct answer
DThe compiler will report 'call to undeclared library function printf'.
Explanation
If '#include <stdio.h>' is omitted, the compiler will not know what 'printf' is, as its declaration resides in that header file. This typically results in an error message like 'call to undeclared library function printf' because the function hasn't been defined in the current scope.
- A
- 29
What is a 'header file' in C programming?
- A
A file used to configure the compiler's settings and options.
- B
A file containing declarations of functions and variables from a library.
- C
A file that stores user input for later processing by the program.
- D
A file that contains the executable machine code of a program.
Correct answer
BA file containing declarations of functions and variables from a library.
Explanation
A header file (ending in .h) typically contains declarations of functions, variables, and macros that are part of a library. Including a header file allows a C program to use the functionality defined in that library.
- A
- 30
What is the purpose of 'manual pages' (man pages) or documentation in programming?
- A
To offer interactive tutorials for learning new programming concepts.
- B
To allow programmers to remotely debug their code in the cloud.
- C
To document how to use functions, libraries, and language features.
- D
To provide a list of common errors and their solutions.
Correct answer
CTo document how to use functions, libraries, and language features.
Explanation
Manual pages (man pages) or documentation serve to provide detailed instructions on how to use various functions, libraries, and features of a programming language, allowing programmers to look up specifics about available functionality.
- A
- 31
Which CS50 library function is used to obtain a string of text input from the user?
- A
read_line
- B
get_integer
- C
get_text
- D
get_string
Correct answer
Dget_string
Explanation
The CS50 library provides a function called 'get_string' specifically to simplify the process of obtaining a string of text (like a word or sentence) as input from the user.
- A
- 32
Which CS50 library function is designed to get an integer number from the user?
- A
read_numeric
- B
get_int
- C
get_float
- D
get_number
Correct answer
Bget_int
Explanation
The CS50 library offers a function named 'get_int' to facilitate getting an integer value (a whole number) as input from the user, making it easier than the standard C methods.
- A
- 33
In C, what is a 'variable' used for?
- A
To link different parts of a program's source code together.
- B
To indicate the entry point of a program's execution flow.
- C
To store a value that can be referred to and changed later.
- D
To define a new function that performs a specific task.
Correct answer
CTo store a value that can be referred to and changed later.
Explanation
A variable in programming is a named storage location in memory that holds a value. This value can be accessed, used, and modified throughout the program's execution, similar to how the 'answer' block stored user input in Scratch.
- A
- 34
What is the purpose of a 'return value' from a function?
- A
To indicate that the function has completed its execution successfully.
- B
To display a message or perform a visual side effect on the screen.
- C
To execute a sequence of commands without interruption.
- D
To hand back a specific piece of data (like text or a number) to the caller.
Correct answer
DTo hand back a specific piece of data (like text or a number) to the caller.
Explanation
A return value is data (e.g., text, numbers) that a function computes or retrieves and then 'hands back' to the part of the code that called it. This allows the calling code to use that value for further processing.
- A
- 35
What is the correct way to declare a variable named 'answer' to store text data in C using the CS50 library?
- A
char answer = get_string("Prompt");
- B
int answer = get_string("Prompt");
- C
string answer = get_string("Prompt");
- D
text answer = get_string("Prompt");
Correct answer
Cstring answer = get_string("Prompt");
Explanation
To store text data in C when using CS50's library, you declare a variable of type 'string' (which is a custom type provided by CS50 for convenience) and assign it the return value of 'get_string'.
- A
- 36
What is the purpose of a semicolon ';' at the end of a line of code in C?
- A
To declare a new variable before it is used in the program.
- B
To separate different parts of a complex expression.
- C
To indicate a comment that the compiler should ignore.
- D
To mark the end of a statement or thought for the compiler.
Correct answer
DTo mark the end of a statement or thought for the compiler.
Explanation
In C, a semicolon ';' is used to terminate a statement. It explicitly tells the compiler that the current instruction or 'thought' is complete, allowing the compiler to correctly parse the code.
- A
- 37
What is the correct 'printf' format code to display a string value?
- A
%f
- B
%i
- C
%d
- D
%s
Correct answer
D%s
Explanation
The format code '%s' is used within 'printf' to indicate a placeholder where a string value should be inserted and displayed.
- A
- 38
What is the correct 'printf' format code to display an integer value?
- A
%c
- B
%f
- C
%s
- D
%i
Correct answer
D%i
Explanation
The format code '%i' (or '%d') is used within 'printf' as a placeholder to display an integer value.
- A
- 39
How do you pass multiple input arguments to a C function like 'printf'?
- A
By enclosing all arguments within a single pair of square brackets.
- B
By defining a new custom data type to encapsulate all arguments.
- C
By using separate sets of parentheses for each argument.
- D
By separating each argument with a comma inside a single pair of parentheses.
Correct answer
DBy separating each argument with a comma inside a single pair of parentheses.
Explanation
To pass multiple inputs to a C function, you separate them with commas within a single pair of parentheses. For example, 'printf("Hello, %s\n", answer);' passes two arguments: the format string and the 'answer' variable.
- A
- 40
What does the 'ls' command do in a terminal window?
- A
It creates a new directory or folder.
- B
It compiles source code into an executable program.
- C
It moves a file from one directory to another.
- D
It lists the files and directories in the current location.
Correct answer
DIt lists the files and directories in the current location.
Explanation
The 'ls' command, short for 'list', is used in a terminal window to display the names of all files and subdirectories that are present in the current working directory.
- A
- 41
Which command is used to change the current directory in a terminal?
- A
open
- B
dir
- C
move
- D
cd
Correct answer
Dcd
Explanation
The 'cd' command, short for 'change directory', is used to navigate and change the current working directory in a command-line interface.
- A
- 42
What is the purpose of the 'rm' command in a terminal?
- A
To copy files from one location to another.
- B
To remove or delete files from the system.
- C
To compile a program from source code.
- D
To rename a file or directory to a new name.
Correct answer
BTo remove or delete files from the system.
Explanation
The 'rm' command, short for 'remove', is used in a terminal to delete files from the file system. It often prompts for confirmation before permanently deleting.
- A
- 43
Which command is used to create a new directory (folder) in a terminal?
- A
add_dir
- B
mkdir
- C
create_dir
- D
new_folder
Correct answer
Bmkdir
Explanation
The 'mkdir' command, short for 'make directory', is used to create a new folder or directory in the file system from the command line.
- A
- 44
How would you use the 'mv' command to move 'hello.c' into a folder named 'src'?
- A
mv hello.c src
- B
move hello.c to src
- C
mv src hello.c
- D
cp hello.c src
Correct answer
Amv hello.c src
Explanation
The 'mv' command (move) takes two arguments: the source file/folder and the destination. So, 'mv hello.c src' would move the file 'hello.c' into the directory named 'src'.
- A
- 45
What is the meaning of 'mv old.c hello.c' in the terminal?
- A
It creates a copy of 'old.c' and names it 'hello.c'.
- B
It moves 'old.c' to a folder named 'hello.c'.
- C
It renames the file 'old.c' to 'hello.c'.
- D
It deletes 'old.c' and creates a new empty 'hello.c'.
Correct answer
CIt renames the file 'old.c' to 'hello.c'.
Explanation
The 'mv' command can also be used for renaming. When used with two file names, it renames the first file ('old.c') to the second file ('hello.c').
- A
- 46
What does 'cp hello.c backup.c' accomplish in the terminal?
- A
It creates a duplicate of 'hello.c' named 'backup.c'.
- B
It compares the contents of 'hello.c' and 'backup.c'.
- C
It renames 'hello.c' to 'backup.c'.
- D
It moves 'hello.c' into a directory named 'backup.c'.
Correct answer
AIt creates a duplicate of 'hello.c' named 'backup.c'.
Explanation
The 'cp' command, short for 'copy', creates a duplicate of the source file ('hello.c') and names the copy as specified ('backup.c'), leaving the original file intact.
- A
- 47
In a terminal, what does 'cd ..' command do?
- A
It navigates to the home directory of the current user.
- B
It lists all subdirectories within the current directory.
- C
It moves up one level to the parent directory.
- D
It reloads the current directory to refresh its contents.
Correct answer
CIt moves up one level to the parent directory.
Explanation
The 'cd ..' command is used to change the current directory to its parent directory, effectively moving 'up' one level in the file system hierarchy.
- A
- 48
What is the primary purpose of 'if' statements (conditionals) in programming?
- A
To perform an action repeatedly for a fixed number of times.
- B
To store multiple values in a single named container.
- C
To execute different blocks of code based on whether a condition is true or false.
- D
To define a new function that can be called from other parts of the program.
Correct answer
CTo execute different blocks of code based on whether a condition is true or false.
Explanation
'If' statements, or conditionals, are fundamental programming constructs that allow a program to make decisions. They execute a specific block of code only if a given Boolean (true/false) condition evaluates to true, providing 'forks in the road' for program flow.
- A
- 49
In C, what is the syntax for checking if two values, 'x' and 'y', are exactly equal?
- A
x = y
- B
x == y
- C
x equals y
- D
x === y
Correct answer
Bx == y
Explanation
In C, the double equal sign '==' is the equality operator, used to test if two values are exactly the same. A single '=' is reserved for assignment.
- A
- 50
Why is it generally considered bad design to use 'if x < y ... else if x > y ... else if x == y ...'?
- A
The 'else if' syntax is not supported in C and will cause an error.
- B
The program will execute too quickly and become unstable.
- C
It is logically redundant to check 'x == y' after the first two conditions.
- D
It makes the code unreadable and impossible to debug effectively.
Correct answer
CIt is logically redundant to check 'x == y' after the first two conditions.
Explanation
It's considered bad design because the 'x == y' condition is logically redundant. If x is not less than y AND x is not greater than y, then x MUST be equal to y. Checking it explicitly wastes computational resources and makes the code unnecessarily longer.
- A
- 51
What does a 'char' data type represent in C?
- A
A single character, such as a letter, number, or symbol.
- B
A single numeric value with a decimal point.
- C
A boolean value (true or false) for logical operations.
- D
A long sequence of text characters.
Correct answer
AA single character, such as a letter, number, or symbol.
Explanation
A 'char' data type in C is used to store a single character, such as 'A', '7', or '$'. It is distinct from a 'string', which is a sequence of characters.
- A
- 52
Why are single quotes ''' used for character literals (e.g., 'y') in C, unlike double quotes '""' for strings?
- A
Single quotes are used for numerical comparisons only.
- B
C distinguishes between a single character and a string of characters.
- C
Single quotes indicate an unchangeable constant value.
- D
Single quotes are an older syntax, primarily for backward compatibility.
Correct answer
BC distinguishes between a single character and a string of characters.
Explanation
C makes a strict distinction between a single character and a string of characters. Single quotes (apostrophes) are used to denote a single character literal (e.g., 'y'), while double quotes are for string literals (e.g., "hello").
- A
- 53
What is the purpose of the logical 'OR' operator ('||') in a C conditional statement?
- A
It assigns the result of one condition to another variable in the statement.
- B
It reverses the truth value of the condition that follows it.
- C
It means that either the left condition OR the right condition must be true.
- D
It checks if both conditions on its sides are simultaneously true.
Correct answer
CIt means that either the left condition OR the right condition must be true.
Explanation
The logical 'OR' operator ('||') evaluates to true if at least one of the conditions it connects is true. For example, 'if (c == 'y' || c == 'Y')' is true if 'c' is either a lowercase 'y' or an uppercase 'Y'.
- A
- 54
What does the '++' operator do when applied to a variable (e.g., 'counter++')?
- A
It subtracts 1 from the variable's current value.
- B
It multiplies the variable's value by 2.
- C
It resets the variable's value to 0.
- D
It adds 1 to the variable's current value.
Correct answer
DIt adds 1 to the variable's current value.
Explanation
The '++' operator is the increment operator in C (and similar languages). It adds 1 to the current value of the variable it is applied to, making it a shorthand for 'counter = counter + 1' or 'counter += 1'.
- A
- 55
What is the primary advantage of using a loop (like a 'for' or 'while' loop) instead of repeating code multiple times?
- A
Loops make the code shorter, more generalizable, and reduce repetition.
- B
Loops execute code faster by optimizing the compiler's output.
- C
Loops enable the program to communicate over a network connection.
- D
Loops allow for conditional execution based on user input.
Correct answer
ALoops make the code shorter, more generalizable, and reduce repetition.
Explanation
Loops are fundamental for reducing repetition in code. They allow a block of code to be executed multiple times, which makes the program more concise, easier to maintain, and more generalizable (e.g., to perform an action 'n' times instead of just 3).
- A
- 56
Which type of loop typically initializes a counter, checks a condition, and updates the counter all on a single line?
- A
do-while loop
- B
infinite loop
- C
while loop
- D
for loop
Correct answer
Dfor loop
Explanation
A 'for' loop is designed to encapsulate the initialization of a loop counter, the condition for continuation, and the update (increment/decrement) of the counter all within its single header line. This makes it very compact for iterative tasks.
- A
- 57
By convention, from what number do programmers often start counting in loops?
- A
1
- B
0
- C
10
- D
5
Correct answer
B0
Explanation
By convention, computer scientists and programmers generally start counting from 0 when working with loops and arrays, for reasons related to memory addressing and indexing that become clearer in later topics.
- A
- 58
What is the purpose of 'Control-C' when running a program in the terminal?
- A
To force-quit or interrupt a running program, especially an infinite loop.
- B
To copy the terminal's output to the clipboard.
- C
To save the current state of the program to a file.
- D
To increase the execution speed of the program for faster results.
Correct answer
ATo force-quit or interrupt a running program, especially an infinite loop.
Explanation
Control-C (often represented as Ctrl+C) is a common keyboard shortcut used in terminal environments to send an interrupt signal to the currently running program. This typically forces the program to terminate, which is very useful for stopping infinite loops or unresponsive applications.
- A
- 59
What is a 'prototype' of a function in C, and why is it sometimes necessary?
- A
A template for automatically generating multiple similar functions.
- B
A simplified version of a function used for debugging purposes only.
- C
A declaration of a function (return type, name, parameters) before its full definition.
- D
The full definition of a function, including its entire code body.
Correct answer
CA declaration of a function (return type, name, parameters) before its full definition.
Explanation
A function prototype is a declaration that tells the compiler about a function's return type, name, and parameters before the function is fully defined. It's necessary when a function is called before its definition in the source file, allowing the compiler to know about its existence and how to use it.
- A
- 60
What is 'scope' in the context of variables in C programming?
- A
The region of code where a variable is accessible and can be used.
- B
The process of converting a variable from one data type to another.
- C
The range of memory addresses where a variable's data is stored.
- D
The maximum value that a variable can hold before overflowing.
Correct answer
AThe region of code where a variable is accessible and can be used.
Explanation
Scope refers to the region of a program where a declared variable is valid and can be accessed or used. Variables generally only exist and are accessible within the curly braces '{ }' where they are defined, and attempting to use them outside that scope will result in an 'undeclared identifier' error.
- A
- 61
What is the purpose of a 'do-while' loop?
- A
To continuously execute a block of code forever, without any stopping condition.
- B
To execute a block of code zero or more times based on a condition.
- C
To execute a block of code at least once, and then repeatedly based on a condition.
- D
To execute a block of code a fixed, predetermined number of times.
Correct answer
CTo execute a block of code at least once, and then repeatedly based on a condition.
Explanation
A 'do-while' loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block as long as a specified Boolean condition remains true. The condition is checked after each iteration.
- A
- 62
When declaring a variable 'n' that is intended to store an integer input from the user within a 'do-while' loop, why must 'int n;' be placed outside the loop?
- A
To ensure 'n' is accessible in the 'while' condition after the 'do' block has executed.
- B
To declare 'n' as a global variable, making it accessible from anywhere in the file.
- C
To allow the variable 'n' to be shared across multiple functions in the program.
- D
To prevent the variable 'n' from being initialized with a default value of zero.
Correct answer
ATo ensure 'n' is accessible in the 'while' condition after the 'do' block has executed.
Explanation
If 'int n;' is declared inside the 'do' block, 'n' would be out of scope and inaccessible to the 'while' condition. Declaring 'int n;' outside the loop, but still within the function, ensures 'n' exists for both the input operation inside the 'do' block and the condition check in the 'while' clause.
- A
- 63
What does the 'return' keyword do in a C function that is specified to have a return type other than 'void'?
- A
It signals to the compiler that the function has no output.
- B
It passes a specified value back to the function that called it.
- C
It allows the function to print directly to the terminal window.
- D
It terminates the program immediately without further execution.
Correct answer
BIt passes a specified value back to the function that called it.
Explanation
The 'return' keyword is used in functions that have a specified return type (e.g., 'int', 'string', 'float' instead of 'void'). It sends a specific value back to the part of the code that called the function, making that value available for use.
- A
- 64
What is meant by a 'magic number' in programming, and why is it generally a bad practice?
- A
A number that is hard-coded directly into the program without explanation.
- B
A number that automatically changes its value during program execution.
- C
A number that causes unpredictable behavior in a program.
- D
A number that represents a secret key for data encryption.
Correct answer
AA number that is hard-coded directly into the program without explanation.
Explanation
A 'magic number' is a hard-coded numerical value that appears directly in code without being defined as a named constant or variable. It's bad practice because it makes the code less readable, harder to understand (its meaning is 'magical'), and difficult to modify consistently if the value needs to change in multiple places.
- A
- 65
How can you protect a variable from accidental modification in C, ensuring its value remains constant?
- A
By using the 'const' keyword during its declaration.
- B
By declaring it with the 'static' keyword.
- C
By placing it in a separate, read-only file.
- D
By only performing addition operations on it.
Correct answer
ABy using the 'const' keyword during its declaration.
Explanation
The 'const' keyword (short for 'constant') is used during variable declaration to specify that the variable's value cannot be changed after its initial assignment. The compiler will enforce this, preventing accidental modifications.
- A
- 66
What is the primary purpose of 'comments' in source code?
- A
To add human-readable notes and explanations that are ignored by the compiler.
- B
To store data that is used by the compiler during compilation.
- C
To temporarily disable sections of code during debugging.
- D
To define special instructions for the operating system.
Correct answer
ATo add human-readable notes and explanations that are ignored by the compiler.
Explanation
Comments are non-executable text within source code, preceded by '//' for single-line comments or '/* ... */' for multi-line comments. Their purpose is to provide human-readable explanations, reminders, or documentation about the code, making it easier for programmers to understand.
- A
- 67
What is 'correctness' in the context of programming?
- A
The adherence to standardized coding conventions and practices.
- B
The aesthetic appeal and readability of the code's layout.
- C
The efficiency of the code in terms of speed and resource usage.
- D
The degree to which the code consistently produces the intended results.
Correct answer
DThe degree to which the code consistently produces the intended results.
Explanation
Correctness is the most fundamental aspect of programming. It refers to whether a program performs its intended task accurately and consistently, producing the expected output for all valid inputs.
- A
- 68
What does 'design' refer to in the evaluation of code quality?
- A
How quickly the program compiles and executes its operations.
- B
How well-structured, efficient, and maintainable the underlying code is.
- C
How visually appealing the program's user interface is.
- D
How thoroughly the code is documented with comments and explanations.
Correct answer
BHow well-structured, efficient, and maintainable the underlying code is.
Explanation
Design, in code evaluation, refers to the qualitative aspects of how well the code is written, structured, and organized. This includes efficiency, good decision-making in implementation, and maintainability, often being more subjective than correctness.
- A
- 69
What is 'style' in the context of code quality?
- A
The absence of any errors or bugs in the program's logic.
- B
The speed at which the program completes its tasks.
- C
The aesthetic presentation, readability, and adherence to coding conventions.
- D
The number of unique features and functionalities implemented in the program.
Correct answer
CThe aesthetic presentation, readability, and adherence to coding conventions.
Explanation
Style relates purely to the aesthetic aspects of code. It focuses on how the code looks, its readability, and whether it follows established formatting and naming conventions, making it easier for humans to skim and understand.
- A
- 70
Which tool in CS50's environment provides feedback on the 'correctness' of your code?
- A
style50
- B
design50
- C
debug50
- D
check50
Correct answer
Dcheck50
Explanation
The 'check50' command-line tool is provided in CS50's environment to automatically test your code against a set of criteria and provide feedback on its correctness, helping to identify many potential bugs.
- A
- 71
What does 'style50' do in the CS50 VS Code environment?
- A
It generates a visual flowchart of your program's execution path.
- B
It analyzes your code for efficient design principles and suggests improvements.
- C
It reformats your code to adhere to CS50's recommended style guidelines.
- D
It automatically corrects all logical errors in your program.
Correct answer
CIt reformats your code to adhere to CS50's recommended style guidelines.
Explanation
The 'style50' tool automatically reformats and suggests improvements to the aesthetic style of your C code to match CS50's recommended conventions, making it more readable and consistent.
- A
- 72
How can you print a 3x3 grid of '#' symbols (bricks) using nested loops in C?
- A
By using a single 'for' loop and a long 'printf' statement.
- B
By manually typing out 'printf("###\n");' three times consecutively.
- C
By creating three separate functions, each printing one row of bricks.
- D
By using two nested 'for' loops, one for rows and one for columns.
Correct answer
DBy using two nested 'for' loops, one for rows and one for columns.
Explanation
A common and effective way to print a grid of symbols is by using nested loops. An outer loop iterates through the rows, and an inner loop iterates through the columns, printing a symbol for each cell.
- A
- 73
What is 'integer overflow' in the context of C programming?
- A
When an integer variable is not properly initialized before being used in calculations.
- B
When an integer variable holds a decimal value instead of a whole number.
- C
When the result of an arithmetic operation exceeds the maximum value an integer type can store.
- D
When a program attempts to divide an integer by zero, causing a crash.
Correct answer
CWhen the result of an arithmetic operation exceeds the maximum value an integer type can store.
Explanation
Integer overflow occurs when an arithmetic operation attempts to create an integer value that is larger (or smaller) than the maximum (or minimum) value that the integer data type can hold. Because of the finite number of bits allocated to the integer, the value 'wraps around' (e.g., from positive max to negative min or to zero), leading to incorrect results.
- A
- 74
Which C data type is typically used for larger integer values to mitigate integer overflow compared to 'int'?
- A
float
- B
long
- C
double
- D
char
Correct answer
Blong
Explanation
The 'long' data type in C is used to store larger integer values compared to the standard 'int'. While 'int' typically uses 32 bits, 'long' often uses 64 bits, allowing it to represent a significantly wider range of numbers and thus delaying integer overflow.
- A
- 75
Why does '1 / 3' in C, when '1' and '3' are 'int' types, result in '0' instead of '0.333...'?
- A
This is a bug in the C compiler that needs to be manually fixed.
- B
C automatically rounds down all division results to the nearest whole number.
- C
The 'printf' function is not configured to display decimal places by default.
- D
Integer division in C truncates the decimal part, keeping only the whole number.
Correct answer
DInteger division in C truncates the decimal part, keeping only the whole number.
Explanation
In C, when you perform division with two integer operands (e.g., 'int / int'), the result is always an integer. This means the decimal part is truncated (cut off), not rounded, so '1 / 3' evaluates to 0, and '3 / 2' evaluates to 1.
- A
- 76
What is 'truncation' in the context of integer division?
- A
The automatic adjustment of an integer's size to fit larger values.
- B
The process of converting a floating-point number to an integer by rounding up.
- C
The error that occurs when dividing by zero, causing the program to stop.
- D
The removal of the decimal part of a number, keeping only the whole number portion.
Correct answer
DThe removal of the decimal part of a number, keeping only the whole number portion.
Explanation
Truncation in integer division refers to the process where any fractional or decimal part of the result is simply discarded, leaving only the whole number part. For example, 3.99 truncated to an integer is 3.
- A
- 77
What is 'floating point precision' in C, and why can it sometimes be problematic?
- A
The exactness with which integers are stored in memory, up to 32 bits.
- B
The exactness of real numbers (floats/doubles) in memory, which is limited by finite bits.
- C
The ability of a program to handle extremely large or small numerical inputs.
- D
The process of converting textual numbers into a binary representation.
Correct answer
BThe exactness of real numbers (floats/doubles) in memory, which is limited by finite bits.
Explanation
Floating-point precision refers to the accuracy with which real numbers (numbers with decimal points) can be represented in a computer's memory. Because computers use a finite number of bits (e.g., 32 or 64) to store these numbers, they can't represent all infinitely many real numbers perfectly, leading to slight inaccuracies or 'imprecision' in calculations.
- A

Ask anything
We've read this whole study set, so questions about it get the best answers.