Programming 1 - Sample Milestone Assignment 2 Questions: Milestone 2 On 10/25/2021, In-Class Released: October 21, 2021
Programming 1 - Sample Milestone Assignment 2 Questions: Milestone 2 On 10/25/2021, In-Class Released: October 21, 2021
Programming 1 - Sample Milestone Assignment 2 Questions: Milestone 2 On 10/25/2021, In-Class Released: October 21, 2021
1
– Are you using the required concepts?
– Are you adhering to standard programming practices discussed in the course (no goto,
auto, break outside a switch, continue; declaring and defining functions, etc).
– IT IS MORE THAN JUST AN OUTPUT MATCHING PROBLEM
• For Problem 4, you need to fix the error and explain what caused the error. You
will only be tested on syntax and semantic errors.
• Solutions to the short answer questions must be SHORT and to the point. A 6
paragraph essay when 4 lines would do means your answer will just be skimmed
through in the interest of grading time (The needs of the many ... )
• You will also receive a Canvas announcement about the milestone assignment with these
details on Saturday, 10/23/2021.
• General Advice: For the sake of completing the assignment on time, please answer
all questions before seeking to verify the answers.
• Making me laugh might gain you points (depends on the quality of the joke).
Topics to study
• Basic C++ Syntax
– Writing a basic C++ program stub: including required libraries, adding namespaces and
writing the main function.
– Simple statements - syntax.
– Comments.
– Reserved words, literals and escape sequences.
– Style guidelines.
• Data types, variables, and sequential execution.
– Naming, declaring and initializing variables.
– Primitive data types.
– Type Conversions - implicit and explicit.
– Arithmetic Operators and operator precedence.
• I/O - printing and reading values from the user.
– cout statements - printing literals and variables.
– Precision for floating point variables.
– Using cin to read data of different kinds.
• Selection statements and loops
– Relational and logical operators.
– Writing simple, multiple and nested if statements.
– switch - case statements.
– while, do-while and for loops
– break and continue statements.
– Overflow and Underflow
2
– Scope of variable
• Writing functions in C++.
– Writing simple functions.
– Passing arguments and returning values. Pass by value and pass by reference.
– Scope of local and global variables.
– Function overloading and default parameters.
• Arrays
– Declaring and initializing an array.
– Reading in array values from the user and printing arrays.
– Basic array operations - looking for a number, math with array elements, etc.
– Passing arrays as parameters to functions.
– Multi dimensional arrays.
• Strings
– Cstrings - arrays of characters.
– Reading strings with getline
– cstring, cctype libraries
– Basic string operations - counting different kinds of characters, finding and replacing
substrings, etc.
– Declaring, initializing, comparing and concatenating strings
– Arrays of strings.
• Studying the topics listed above will be enough to pass the test. To get a 100, you would be
required to study everything on the notes.
• You don’t need to study from outside sources. The test is made entirely from the notes, class
examples, exercises, quizzes and assignments.
Sample Questions
1. Which of the following is NOT a C++ reserved word?
(a) float
(b) function
(c) void
(d) return
Answer: function, everything else is a keyword.
2. Given an array of doubles starting at address 5000, what is the starting address of element 7
of the array?
(a) 5000
(b) 5048
(c) 5056
3
(d) 5064
Answer: 5000 + 7x8 = 5056
3. Which of the following functions will return 0 (false) for the character ‘A’ ?
(a) isalnum
(b) isprint
(c) islower
(d) islapha
Solution:
#include <iostream>
#include<cstring>
using namespace std;
changeE(st);
cout<<"Output:\n"<<str<<endl;
return 0;
}
void changeE(char str[])
{
for(int i=0; i<strlen(st); i++)
{
if(str[i]==’e’)
str[i] = ’3’;
}
}
5. Write a C++ function that accepts a double array and its size. This array represents the side
lengths of a bunch of squares. Calculate areas of squares, replace the array values with their
squares, and return their sum. In the main function, create an array of 5 doubles. Read in
4
the values for the array, and then call the function. Finally, print the array and the returned
sum. You do not have to account for floating point precision. You may use the iostream and
the cmath libraries.
Sample Run:
Solution:
#include <iostream>
using namespace std;
int main()
{
double arr[5];
cout<<"Enter the array values:\n";
for(int i=0; i<5; i++)
{
cin>>arr[i];
}
return 0;
}
return sum;
}
6. Figure out the output generated by the following code snippet:
5
int mat[3][3] = {{1,5,19},{6,-2,10},{12,8,5}};
int sum=0;
for(int i=0;i<3;i++)
sum += mat[i][i];
cout<<"The sum is "<<sum<<endl;
Solution: The sum is 4
The code iterates through the rows of the matrix, and adds up the elements where the row
number is equal to the column number. 1 + -2 + 5 = 4.
7. What is the difference between the size and the capacity of a string?
Answer:
The size of the string is the number of characters currently in the string. The capacity is the
number of characters the string is capable of holding without going over bounds.
Similarly work through the class examples and homeworks. The problems on the test are heavily
based on class examples, homework problems or practice problems.
If you have questions, ask during help sessions or after class. Help Sessions are not just for homework
help. They’re a vital resource when you don’t have regular in-person contact with your instructor
or TA/LA.
6
For this problem, you don’t need to do error checking. You only need to solve
the specific problem, assuming everything else goes well.
• 5 minutes for the code debugging question - we’re only looking for syntax errors
- errors that would stop CLion from compiling your program. Do not try to
find the output for the program, or even see if the program makes sense line by
line. Syntax errors are usually restricted to that line.
You need to tell me what the error is and how it can be fixed - or show a
line with the error fixed.
We’re aware errors propagate. So, if a future line will have an error due to
previous line, and you’ve already identified and fixed the first, then this one
wouldn’t count. For example, if there is an error in a line with a variable dec-
laration, then fixing that line would fix all the future “uninitialized variable”
errors.
• 4 minutes per short answer question - Here, we’re interested in what you know
about the concept.
• The objective of the test is not to be 100% right. It mimics the programming
interview, where a “quick and dirty” solution is good enough for the first part of
the process - your initial thoughts - that demonstrates your level of knowledge
and understanding of programming.