Coding and comprehension
- [10] Identify at least one of each of the following in the below code:
- reserved keywords
- preprocessing directives
- variable definitions
- variable declarations
- variable initialization
- user input
- output
- function
#include
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1, v2;
std::cin >> v1 >> v2;
std::cout << "The sum of " << v1 << " and " << v2
<< " is " << v1 + v2 << std::endl;
return 0;
}
- [5]
What is the output of the above code after each of these inputs?
- 4 7
- 17 23
- 5.6 3.2
- [10]
Write a program to print "Hello, World" on the standard output.
- [9] Indicate which, if any, of the following output statements, are legal.
std::cout << "/*";
std::cout << "*/";
std::cout << /* "*/" */;
After you've predicted what will happen, test your answer by compiling a program with these three statements. Indicate the differences between your predicted output and what you learn from the compiler in your answer.
Data types and variables
- [2]
What is the difference between an
int, a long, and a short value?
- [2]
What is the difference between an
unsigned and a signed type?
- [2]
If a
short on a given machine has 16 bits then what is the largest number that can be assigned to a short? To an unsigned short?
- [2]
What value is assigned if we assign 100,000 to a 16-bit
unsigned short? What value is assigned if we assign 100,000 to a plain 16-bit short?
- [8]
Using escape sequences, write a program to print 2M followed by a newline. Modify the program to print 2, then a tab, then an M, followed by a newline.
- [6]
Distinguish between an lvalue and an rvalue; show examples of each.
- [4]
What, if any, are the differences between the following definitions:
int month = 9, day = 7;
int month = 09, day = 07;
- [5]
What is the difference between the following two statements?
int i = 10, r = i;
int i = 10, &r = i;