Programming I (C++)
Useful Resources
Practice Questions
Click on the questions below to reveal the answer
Describe the fundamental data types: char, int, float, and bool.
Fundamental data types are basic types implemented directly by the language that represent the basic storage units supported natively by most systems. They can mainly be classified into:
- Character types: They can represent a single character, such as 'A' or '$'. The most basic type is char, which is a one-byte character.
- Numerical integer types: They can store a whole number value, such as 7 or 1024. They exist in a variety of sizes, and can either be signed or unsigned, depending on whether they support negative values or not.
- Floating-point types: They can represent real values, such as 3.14 or 0.01, with different levels of precision, depending on which of the three floating-point types is used.
- Boolean type: The boolean type, known in C++ as bool, can only represent one of two states, true or false.
What does the #include
directive do?
The include directive instructs the preprocessor to paste the text of the given file into the current file.
So a #include <stdio.h>
statement will be replaced by the contents of stdio.h.
What function does every C/C++ program require? Why is it required?
The main()
function.
When the operating system runs a program in C/C++, it passes control of the computer over to that program. The key point is that the operating system needs to know where inside your program the control needs to be passed. In the case of a C/C++ language program, it’s the main()
function that the operating system is looking for.
Write a program that prints "Hello World!" in C++
#include <iostream>
int main()
{
std::cout << "Hello World!";
return 0;
}
What does the using namespace std
declaration do?
All the elements in the standard C++ library are declared within what is a called a namespace: the namespace std.
The using namespace std;
declaration allows all elements in the std namespace to be accessed in an unqualified manner (without the std:: prefix).
What is a constant? What is a literal?
Constants are expressions with a fixed value.
Constants are declared with the const
keyword:
const double pi = 3.1415926;
const char tab = '\t';
Literals are the most obvious kind of constants. They are used to express particular values within the source code of a program.
Literals include integer numerals, floating-point numerals, characters, and strings.
What does the #define
statement do?
#define identifier replacement
After this directive, any occurrence of identifier in the code is interpreted as replacement, where replacement is any sequence of characters (until the end of the line). This replacement is performed by the preprocessor, and happens before the program is compiled.
What does the sizeof() operator do?
Get the size of a char
and assign it to an integer variable.
This operator accepts one parameter, which can be either a type or a variable, and returns the size in bytes of that type or object:
int x = sizeof(char);
What's the difference between the operators =
and ==
?
The assignment operator assigns a value to a variable.
int x = 5; // assigns the value 5 to x
The equality operator tests if two values are equal and returns true or false.
if( 7 == 5 ) // evaluates to false
Write a program that reads an integer value from the user, stores it in a variable, and prints it to stdout.
#include <iostream>
using namespace std;
int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
return 0;
}
Write a program that uses string
and getline
to read in a user's name and print it to stdout.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystr;
cout << "What's your name? ";
getline (cin, mystr);
cout << "Hello " << mystr << ".\n";
return 0;
}
Write a program that reads an integer from the user, tests if it's positive, negative, or neither and prints a message telling the user which it is.
#include <iostream>
using namespace std;
int main ()
{
int x;
cout << "Please enter an integer value:";
cin >> x;
if (x > 0)
cout << "x is positive";
else if (x < 0)
cout << "x is negative";
else
cout << "x is 0";
return 0;
}
Write a program that uses a while
loop to count down from 10 to 0 and prints the countdown to stdout.
#include <iostream>
using namespace std;
int main ()
{
int n = 10;
while (n>0)
{
cout << n << ", ";
--n;
}
cout << n;
return 0;
}
Write a program that uses a for
loop to count down from 10 to 0 and prints the countdown to stdout.
#include <iostream>
using namespace std;
int main ()
{
int n = 10;
for (int n=10; n>0; n--)
cout << n << ", ";
cout << n;
return 0;
}
What do the break
and continue
statements do?
break
leaves a loop, even if the condition for its end is not fulfilled. It can be used to end an infinite loop, or to force it to end before its natural end.
The continue
statement causes the program to skip the rest of the loop in the current iteration, as if the end of the statement block had been reached, causing it to jump to the start of the following iteration.
Write a program that uses a function to add two numbers, assigns the result to an integer variable, and prints the contents of the variable to stdout.
#include <iostream>
using namespace std;
int addition (int a, int b)
{
return a+b;
}
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
Write a program that sums the values held in the following array and prints them to stdout.
int foo [] = {16, 2, 77, 40, 12071};
#include <iostream>
using namespace std;
int main ()
{
int foo [] = {16, 2, 77, 40, 12071};
int n, result=0;
for ( n=0 ; n<5 ; ++n )
result += foo[n];
cout << result;
return 0;
}
Using the following struct, write a program that reads the title and the year of a movie from the user and stores it in the_movie
.
struct movies_t { string title; int year; } the_movie;
#include <iostream>
#include <string>
using namespace std;
struct movies_t {
string title;
int year;
} the_movie;
int main ()
{
getline (cin,the_movie.title);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> the_movie.year;
return 0;
}
What are the values held in firstvalue and secondvalue after each assignment using pointers p1 and p2 below:
int firstvalue = 5, secondvalue = 15;
int * p1, * p2;
p1 = &firstvalue;
p2 = &secondvalue;
*p1 = 10;
*p2 = *p1;
p1 = p2;
*p1 = 20;
#include <iostream>
using namespace std;
int main ()
{
int firstvalue = 5, secondvalue = 15;
int * p1, * p2;
p1 = &firstvalue; // p1 = address of firstvalue
p2 = &secondvalue; // p2 = address of secondvalue
*p1 = 10; // firstvalue = 10
*p2 = *p1; // secondvalue = 10
p1 = p2; // p1 = p2 (value of pointer is copied)
*p1 = 20; // secondvalue = 20
cout << "firstvalue is " << firstvalue << '\n';
cout << "secondvalue is " << secondvalue << '\n';
return 0;
}
This program outputs:
firstvalue is 10
secondvalue is 20