0
0
C++programming~10 mins

Using cout for output in C++ - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print "Hello, World!" using cout.

C++
#include <iostream>
int main() {
    std::[1] << "Hello, World!" << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
Acerr
Bcin
Ccout
Dclog
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cin' instead of 'cout' for output.
Using 'cerr' which is for error output, not normal output.
2fill in blank
medium

Complete the code to print the value of variable x using cout.

C++
#include <iostream>
int main() {
    int x = 5;
    std::cout << "Value: " << [1] << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
Astd::endl
B"x"
C5
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the variable name in quotes, which prints the text instead of the value.
Using a number directly instead of the variable.
3fill in blank
hard

Fix the error in the code to correctly print "Sum: 15" using cout.

C++
#include <iostream>
int main() {
    int a = 7, b = 8;
    std::cout << "Sum: " [1] a + b << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
A+
B<<
C>>
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '<<' between string and variables.
Using '>>' which is for input, not output.
4fill in blank
hard

Fill both blanks to print the product of a and b with a message.

C++
#include <iostream>
int main() {
    int a = 4, b = 6;
    std::cout [1] "Product: " [2] a * b << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
A<<
B+
C*
D>>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' or '*' operators instead of '<<' for output chaining.
Using '>>' which is for input.
5fill in blank
hard

Fill all three blanks to print the sum and difference of x and y.

C++
#include <iostream>
int main() {
    int x = 10, y = 3;
    std::cout [1] "Sum: " [2] x + y << ", Difference: " [3] x - y << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
A<<
B+
D>>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '<<' for output chaining.
Using '>>' which is for input.
Not adding numbers correctly.