Shared Flashcard Set

Details

LEARN C++ Flashcards
Flashcards to go along with LearnCpp.com
16
Other
Undergraduate 1
02/28/2014

Additional Other Flashcards

 


 

Cards

Term
What is the difference between a statement and an expression?
Definition
A statement is a “complete sentence” that tells the compiler to perform a particular task. An expression is a mathematical entity that evaluates to a value. Expressions are often used inside of statements.
Term
What is the difference between a function and a library?
Definition
A function is a collection of statements that executes sequentially, typically designed to perform a specific job. A library is a collection of functions that is meant to be reused by many programs.
Term
 What symbol do statements in C++ end with?
Definition
The semicolon (;)
Term
What does the symbol // do?
Definition
the symbol // allows you to make a comment line
Term

What values does this program print?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int x = 5;
x = x - 2;
cout << x << endl; // #1
 
int y = x;
cout << y << endl; // #2
 
// x + y is an r-value in this context, so evaluate their values
cout << x + y << endl; // #3
 
cout << x << endl; // #4
 
int z;
cout << z << endl; // #5
Definition

#1 The program outputs 3. x – 2 evaluates to 3, which was assigned to x.


#2 The program outputs 3. y was assigned the value of x, which evaluated to 3.


#3 The program outputs 6. x + y evaluates to 6. There was no assignment here.


#4 The program outputs 3. The value of x is still 3 because it was never reassigned.


#5 The output is indeterminate . z is an uninitialized variable.


Term

1) What’s wrong with this program fragment?

1
2
3
4
5
6
7
8
9
10
void multiply(int x, int y)
{
    return x * y;
}
 
int main()
{
    cout << multiply(4, 5) << endl;
    return 0;
}
Definition
multiply() is defined as returning void, which means it can’t return a value. Since the function is trying to return a value, this function will produce a compiler error. The function should return an int.
Term

2) What’s wrong with this program fragment?

1
2
3
4
5
6
7
8
9
10
int multiply(int x, int y)
{
    int product = x * y;
}
 
int main()
{
    cout << multiply(4, 5) << endl;
    return 0;
}
Definition
multiply() calculates a value and puts the result in a variable, but never returns the value to the caller. Because there is no return statement, and the function is supposed to return an int, this will produce a compiler error.
Term

3) What value does the following program fragment print?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int add(int x, int y, int z)
{
    return x + y + z;
}
 
int multiply(int x, int y)
{
    return x * y;
}
 
int main()
 
{
    cout << multiply(add(1, 2, 3), 4) << endl;
    return 0;
}
Definition
multiply is called where x = add(1, 2, 3), and y = 4. First, the CPU resolves x = add(1, 2, 3), which returns 1 + 2 + 3, or x = 6. multiply(6, 4) = 24, which is the answer.
Term
4) Write a function called doubleNumber() that takes one integer parameter and returns double it’s value.
Definition
1
2
3
4
int doubleNumber(int x)
{
    return 2 * x;
}
Term
Write a complete program that reads an integer from the user , doubles it using a function called doubleNumber() , and then prints the doubled value out to the console.
Definition
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
 
int doubleNumber(int x)
{
    return 2 * x;
}
 
int main()
{
    using namespace std;
    int x;
    cin >> x;
    cout << doubleNumber(x) << endl;
    
Term
What is a literal?
Definition
A Literal is a number.
Term
What is an Operand?
Definition
An operand is the objects of an expression that is acted upon, such as literals, variables, and functions.
Term
What do operators do and what are some examples of an operator?
Definition

Operators tell how to combine the operands to produce a new result


addition (+), subtraction (-), multiplication (*), and division (/). Assignment (=)

Term
What is a Unary operator?
Definition
Unary operators act on one operand. An example of a unary operator is the – operator. In the expression -5, the – operator is only being applied to one operand
Term
What is a Binary operator?
Definition

Binary operators act on two operands (known as left and right). An example of a binary operator is the + operator.

In the expression 3 + 4,

Supporting users have an ad free experience!