Skip to the content of the web site.

Interview questions

IEEE Spectrum Magazine    MIT Technology Review    Wired Magazine    EDN Network    EE Times    Skeptic's Guide to the Universe

1. How do you store a true-false value in C++?

The easiest way is using a variable of type bool. This, unfortunately, occupies one full byte, so another means of storing a true-false value is to use bits. For example, a short could store 16 different true-false values. In this example, we are using the 3rd bit:

// You can run this code at http://cpp.sh/87el3q
#include <iostream>

// Function declarations
int main();

// Function definitions
int main() {
    int n;
    bool is_positive{ false }; // Initialized to false:

    std::cout << "Enter a number: ";
    std::cin >> n;

    if ( n >= 0 ) {
        is_positive = true;  // Set to true
    } else {
        is_positive = false; // Set to false
    }

    short const BIT_3 = (1 << 3); // 00000000 00001000    
    short bits{ 0 };    // Initialized to  00000000 00000000

    std::cout << "Enter another number: ";
    std::cin >> n;

    if ( n >= 0 ) {
        bits |=  BIT_3;     // Set bit 3 to 1
    } else {
        bits &= ~BIT_3;     // Set bit 3 to 0
    }

    // Test value
    if ( is_positive && (bits & BIT_3) ) {
        std::cout << "Both integers are positive" << std::endl;
    } else if ( !is_positive && !(bits & BIT_3) ) {
        std::cout << "Both integers are negative" << std::endl;
    } else {
        std::cout << "One integer is positive and the other negative" << std::endl;
    }

    return 0;
}

2. What does consider C++ consider to be true or false in either a conditional statement, a while loop, a do-while loop, or the second field of a for loop?

The result of any Boolean-valued operator, logical operator or Boolean variable will be either true or false, but if it is an integer type, floating-point number type or pointer, then 0, 0.0 or -0.0 or nullptr is considered false and any other value is considered to be true.

3. What is the scope of a parameter?

The scope of a parameter is the entire function body.

There is one small exception to this: if a local variable is declared to have the same identifier as the parameter, the parameter cannot be accessed until the local variable goes out of scope. Such a parameter is said to be shadowed by the local variable. For example, this program shows the parameter x shadowed by a local variable x.

#include <iostream>

// Function declarations
int main();
void f( int x );

// Function definitions
void f( int x ) {
    std::cout << "Outside the scope of the local variable 'x', x = " << x << std::endl;

    if ( x > 0 ) {
        std::cout << "Outside the scope of the local variable 'x', x = " << x << std::endl;
        int x{ 7 };
        // The parameter is now inaccessible
        std::cout << "Within the scope of the local variable 'x', x = " << x << std::endl;
    } else {
        std::cout << "Outside the scope of the local variable 'x', x = " << x << std::endl;
    }

    std::cout << "Outside the scope of the local variable 'x', x = " << x << std::endl;
}

int main() {
    std::cout << "Calling f( 3 ):" << std::endl;
    f( 3 );

    std::cout << "Calling f( 9 ):" << std::endl;
    f( 9 );

    return 0;
}

2. Keywords

2.1 What is the purpose of the inline keyword?

The inline keyword is used to modify a function, hinting to the compiler that rather than using a function call, the compiler should simply replace the function call with code equivalent to the function body.

The compiler is not required to follow through on this hint, and in some cases, the compiler may even inline a function even if the user does not specify the function to be inlined.

2.2 What are the two ways the sizeof operator can be used?

The argument can be a type, in which case, the type must follow the sizeof operator in parentheses. The operator and its operand evaluate to the number of bytes that would be allocated to an instance of that type.

Alternatively, the the argument can be an identifier (other than one that identifies a function). The operator and its operand evaluate to the number of bytes that are allocated to that identifier.

3. Types

3.1 What is the difference between short, int and long?

The type short indicates that two bytes should be allocated for the variable, int indicates four bytes, and long indicates eight bytes.

Very rarely a compiler may allocate different amounts, but this is exceptionally rare.

3.2 How can you determine the amount of memory used by a type (be it a primitive data type, a structure or a class)?

Use the sizeof operator with parentheses to determine the memory allocated for an instance of the given type.

3.3 Describe integer overflow and underflow.

Arithmetic underflow occurs when the result of an integer operation results in a value less than the smallest possible integer that the type in question can store. Arithmetic overflow occurs when the result is greater than the largest possible integer that the type can store. The resulting integer value is invalid.

3.4 Describe floating-point overflow and underflow.

If a floating-point operation results in a value greater in absolute value than that which can be stored by the floating-point representation, the value saved is a special floating-point number described as inf or -inf, although it is better to think of this as any possible number greater than the greatest number that can be stored. When this occurs, it is said to be a floating-point overflow. The following are true: inf + x == inf and -inf + x == -inf for any finite floating-point number x, inf + inf == inf and -inf + (-inf) == -inf, but inf - inf is undefined.

If a floating-point operation results in a value smaller in absolute value than the smallest non-zero number which can be stored by floating-point representation, the value saved is +0 or -0. This is floating-point underflow. Again, +0 represents all real numbers too small to be represented by the smallest positive floating-point number.

4. Operators

4.1 What are the three operands of the ternary conditional operator ?:?

The ternary conditional operator a ? b : c takes a logical expression as the first operand, and the operator evaluates to the second operand if the conditional statement is true, and evaluates to the third operand otherwise.

4.2 If n is declared to be int, does the statement ((n != 0) ? 1000/n : 0) result in a division-by-zero error if n == 0? Why or why not?

The ternary conditional operator uses lazy evaluation: it does not evaluate the operand that is not the result, so if n == 0, the expression 1000/n is never evaluated; instead, the entire expression simply evaluates to 0.