Introduction to Programming
- 57Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
Define programming in simple terms as described in the lecture.
57 questions
- 1
Define programming in simple terms as described in the lecture.
Model answer
Programming is the process of getting a computer to complete a specific task without making mistakes by giving it very specific instructions.
Explanation
The lecture explains programming as preparing an instructional program for a device, simplified as instructing a computer to perform a task correctly.
- 2
What language does a computer understand directly?
Model answer
Machine code, which is binary (a sequence of 1s and 0s).
Explanation
Computers only understand machine code, a numerical language in binary.
- 3
What is the role of a programming language?
Model answer
A programming language acts as a translator that converts human-readable instructions into machine code.
Explanation
Programming languages serve as a middleman to translate programs into machine code.
- 4
Explain the difference between high-level and low-level programming languages.
Model answer
Low-level languages are closer to machine code and resemble binary instructions, while high-level languages have more abstraction and are easier for humans to understand.
Explanation
Low-level languages (e.g., assembly) are closer to binary; high-level languages (e.g., Java, Python) are more abstract.
- 5
Name two examples of general-purpose programming languages and two examples of specific-purpose languages mentioned.
Model answer
General-purpose: Python and Java. Specific-purpose: RobotC (for robots) and HTML/CSS (for websites).
Explanation
Python and Java are general-purpose; RobotC and HTML/CSS are designed for specific tasks.
- 6
What does IDE stand for and what is its main purpose?
Model answer
IDE stands for Integrated Development Environment. Its main purpose is to provide a graphical interface for writing, running, and debugging code, handling compilation and interpretation.
Explanation
IDEs facilitate coding by offering tools like error checking, autofill, and project hierarchy.
- 7
What is syntax in programming?
Model answer
Syntax is the set of grammar rules that must be followed when writing code in a specific programming language.
Explanation
Just like natural languages, programming languages have rules (syntax) that, if broken, cause errors.
- 8
What is the purpose of the console in an IDE?
Model answer
The console is a text interface used to output information from a program, primarily for the developer's use, not the end user.
Explanation
The console displays output via print statements and is a developer tool for debugging and viewing program results.
- 9
What does a print statement do?
Model answer
A print statement outputs text to the console.
Explanation
The print statement is a fundamental command that prints text to the console.
- 10
What is concatenation in programming?
Model answer
Concatenation is the operation of joining strings together, often using a plus sign, and can combine strings with other data types like integers.
Explanation
Concatenation allows combining multiple strings and variables into one output.
- 11
What is the difference between
"4"and4in code?Model answer
"4"is a string (text), while4is an integer (number). Performing math on a string will cause an error.Explanation
Quotation marks denote a string; without them, it's a numeric value. Computers treat them differently.
- 12
Name the four basic arithmetic operators and the modulus operator.
Model answer
Addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).
Explanation
Basic math operators are standard; modulus returns the remainder of division.
- 13
What does the modulus operator do? Give an example.
Model answer
Modulus returns the remainder of a division. For example, 10 % 3 returns 1.
Explanation
Modulus is useful for checking even/odd and other remainder-based operations.
- 14
What is a variable?
Model answer
A variable is a storage location in memory that holds a value, identified by a name, and can be referenced and manipulated.
Explanation
Variables are like labeled boxes that store information.
- 15
List five primitive variable types mentioned in the lecture.
Model answer
int, boolean, float, double, string, char.
Explanation
Primitive types include integers, booleans, floating-point numbers, strings, and characters.
- 16
What is the range of an integer variable in Java?
Model answer
From -2,147,483,648 to 2,147,483,647.
Explanation
Integers have a specific range; they cannot hold decimal values.
- 17
What values can a boolean variable hold?
Model answer
True or false.
Explanation
Booleans are binary and used in conditions.
- 18
What is the difference between a float and a double?
Model answer
A float has 32-bit precision, while a double has 64-bit precision, allowing doubles to store more decimal places.
Explanation
Doubles are more precise than floats.
- 19
What is a char variable?
Model answer
A char variable holds a single character.
Explanation
Chars are used for single characters, like a key press.
- 20
What happens if you try to reference a variable that has not been initialized?
Model answer
It results in a NullPointerException error.
Explanation
Accessing an uninitialized variable causes a runtime error.
- 21
Explain how two variables can point to the same memory location.
Model answer
Setting one variable equal to another does not create a new memory space; it makes the second variable point to the same location as the first.
Explanation
Assignment by reference shares the same stored value.
- 22
What is the camelCase naming convention for variables?
Model answer
The first word starts with a lowercase letter, and each subsequent word starts with an uppercase letter, with no spaces (e.g., playerScore).
Explanation
CamelCase improves readability by identifying word boundaries.
- 23
What is an if statement?
Model answer
An if statement checks a condition (boolean) and executes a block of code only if the condition is true.
Explanation
Conditional statements change program flow based on conditions.
- 24
What is the purpose of an else if statement?
Model answer
An else if statement provides an alternative condition to test if the preceding if (or else if) condition was false.
Explanation
Else if allows multiple conditions to be checked in sequence.
- 25
When does an else statement execute?
Model answer
An else statement executes when all preceding if and else if conditions are false.
Explanation
Else acts as a catch-all default case.
- 26
What is a switch statement and when is it useful?
Model answer
A switch statement is an alternative to many if-else statements, evaluating a variable against multiple cases and executing the matching case's code. It is useful for reducing code clutter.
Explanation
Switch statements use cases, colons, break statements, and a default case.
- 27
What are the three parts of a for loop?
Model answer
An integer initialization, a condition to continue, and an operation to modify the integer after each iteration.
Explanation
For loops repeat code a set number of times based on these parts.
- 28
What is the difference between a for loop and a for each loop?
Model answer
A for loop iterates based on an index and condition, while a for each loop iterates over each element in an array or list directly.
Explanation
For each is used for iterating through collections.
- 29
How does a while loop differ from a do-while loop?
Model answer
A while loop checks the condition before executing the block, so it may not execute at all. A do-while loop executes the block at least once before checking the condition.
Explanation
Do-while guarantees at least one execution.
- 30
What is an infinite loop and how can it occur?
Model answer
An infinite loop occurs when the loop's termination condition is never met, causing it to run forever. For example, a for loop starting at 0 with condition i < 10 but decrementing i would be infinite.
Explanation
Infinite loops can crash the program or cause stack overflow.
- 31
What is an array?
Model answer
An array is a data structure that stores a fixed-size sequential collection of elements of the same type.
Explanation
Arrays are lists of related data with a fixed size after initialization.
- 32
Explain how indexing works in an array.
Model answer
Array indices start at 0. The first element is at index 0, the second at 1, and so on. Accessing an index beyond the array's size causes an out-of-bounds error.
Explanation
Zero-based indexing is a common programming quirk.
- 33
Can the size of an array be changed after it is created?
Model answer
No, the size of an array is fixed once it is initialized.
Explanation
Array sizes are final; to store more elements, you must create a new array.
- 34
What is a two-dimensional (2D) array?
Model answer
A 2D array is an array of arrays, like a matrix. It is indexed by row and then column (e.g., array[row][column]).
Explanation
2D arrays are useful for representing grids or tables.
- 35
What is an ArrayList and how is it different from a regular array?
Model answer
An ArrayList is a growable array that automatically expands when needed (starting with size 10), unlike a regular array which has a fixed size.
Explanation
ArrayLists handle dynamic resizing automatically.
- 36
What is a dictionary (or map) in programming?
Model answer
A dictionary stores key-value pairs, where each key is unique and is used to look up the corresponding value.
Explanation
Dictionaries provide associative lookup rather than positional indexing.
- 37
What is a function?
Model answer
A function is a reusable segment of code that can be called by name, optionally taking arguments and returning a value.
Explanation
Functions encapsulate code for reuse and organization.
- 38
What are arguments in a function?
Model answer
Arguments are values passed into a function for the function to use or manipulate.
Explanation
Arguments allow functions to operate on different data.
- 39
What does it mean for a function to return a value?
Model answer
A function returns a value when it produces a result that can be assigned to a variable or used in an expression.
Explanation
Return values allow functions to output data for further use.
- 40
What is a void function?
Model answer
A void function is one that does not return any value; it simply executes its code.
Explanation
Void functions are used for actions like printing.
- 41
What is an import statement used for?
Model answer
An import statement allows a program to use functions from external libraries or packages.
Explanation
Importing saves time by using pre-built code.
- 42
When creating a function that returns a value, what must be ensured?
Model answer
All possible code paths must return a value of the correct type, and the function must eventually reach a return statement.
Explanation
Failure to return in all paths causes a compile-time error.
- 43
What is a syntax error?
Model answer
A syntax error occurs when code violates the grammar rules of the programming language, like missing a semicolon.
Explanation
Syntax errors are usually easy to fix and prevent code from running.
- 44
What is a runtime error?
Model answer
A runtime error occurs during program execution, often due to conditions like infinite loops or null pointer exceptions.
Explanation
Runtime errors crash the program when something goes wrong at runtime.
- 45
What is a logic error?
Model answer
A logic error is when the code runs without errors but produces incorrect output due to flawed reasoning (e.g., using multiplication instead of addition).
Explanation
Logic errors are hardest to debug because there are no error messages.
- 46
Describe two debugging strategies mentioned.
Model answer
Using print statements to output variable values at key points, and using breakpoints to pause execution and inspect state.
Explanation
Print statements and breakpoints help locate logic errors.
- 47
How can commenting out code help in debugging?
Model answer
Commenting out code temporarily disables it without deletion, allowing isolation of problematic sections.
Explanation
Comments are ignored by the compiler, so they can be used to test specific parts.
- 48
What is the recommended approach to avoid errors in large projects?
Model answer
Run and test code frequently, save backups (e.g., using version control like GitHub), and plan before coding.
Explanation
Frequent testing and backups help catch errors early.
- 49
What is a linear search?
Model answer
A linear search checks each element in a list sequentially from start to end until the target is found or the list ends.
Explanation
Linear search works on unsorted lists but is O(n) in worst case.
- 50
What is a binary search and what prerequisite must the list meet?
Model answer
A binary search repeatedly divides the search interval in half, requiring the list to be sorted. It has O(log n) efficiency.
Explanation
Binary search is much faster than linear on sorted data.
- 51
What is recursion in programming?
Model answer
Recursion is when a function calls itself to solve a problem by breaking it into smaller sub-problems.
Explanation
Recursive functions need a base case to stop calling themselves.
- 52
What is a base case in recursion?
Model answer
A base case is a condition that stops the recursion by returning a value without making another recursive call.
Explanation
Without a base case, recursion would cause infinite recursion and a stack overflow.
- 53
Explain the concept of a stack in relation to recursion.
Model answer
A stack is a LIFO (Last In, First Out) data structure that stores function calls. Each recursive call is placed on the stack, and when the base case is reached, calls are resolved from top to bottom.
Explanation
The stack management is crucial for recursion; infinite recursion fills the stack and causes overflow.
- 54
What is pseudocode?
Model answer
Pseudocode is a simplified, informal way of planning a program's logic without worrying about syntax.
Explanation
Pseudocode helps in designing the program before writing actual code.
- 55
Name three pseudocode strategies discussed.
Model answer
Flowcharts (graphical representation), chronological write-up (step-by-step description), and feature planning (listing features and required functions).
Explanation
These strategies help plan at different levels of detail.
- 56
What factors should be considered when choosing a programming language for a project?
Model answer
The project's purpose (e.g., web development, general application), language level, available libraries, and personal preference.
Explanation
Different languages excel in different areas; comfort and syntax preference also matter.
- 57
Name two websites recommended for practicing coding challenges.
Model answer
CodingBat and HackerRank.
Explanation
These sites offer problems in various languages to improve skills.

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