This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Formatting”.
1. Which is used for formatting purpose in c++?
a) Whitespace
b) Container
c) &
d) Vector
View Answer
Explanation: Whitespace is a term that refers to characters like spaces, tabs, and newlines that are used for formatting purposes.
2. How many number of spaces should be set in default tab?
a) 1
b) 2
c) 3
d) 4
View Answer
Explanation: The default number of spaces is 4 in programming.
3. What can be improved by formatting the source code?
a) Memory
b) Address
c) User interface
d) Iterator
View Answer
Explanation: By formatting your code, the user can easily understand it and can upgrade it without any complexity.
4. Choose the correct formatted code.
a)
cout << "Hai" << "Good morning";
b)
cout << "Hai" << "Good morning" << endl;
c) cout<<"Hai";
d) cout>>"Hai";
View Answer
Explanation: If a long line that is broken into pieces is broken with an operator, the operator should be placed at the end of the line, not the start of the next line.
5. Choose the correct formatted code.
a) int a = 5;
b) int a=5;
c) int a =5;
d) int a5;
View Answer
Explanation: Variable initialization should be done with one space on left and right of equal operator.
6. Choose the correct formatted code.
a)
int main(){ cout << "5";}
b)
int main() { cout << "5"; }
c)
int main() { cout << "5";}
d)
main() { cin>>5;View Answer
Explanation: The braces that tell where a function begins and ends should be aligned with the function name and be on their own lines.
7. Which function allows you to set minimum width for the next input?
a) setfill
b) setw
c) setwidth
d) setheight
View Answer
Explanation: setw function of iomanip header file allows you to set minimum width for the next input.
8. What will be the output of the following C++ code?
#include <iostream>
#include <iomanip>
using namespace std;
void showDate(int m, int d, int y)
{
cout << setfill('0');
cout << setw(2) << m << '/'
<< setw(2) << d << '/'
<< setw(4) << y << endl;
}
int main()
{
showDate(1, 1, 2013);
return 0;
}
a) 1,1,2013
b) 2013
c) 01/01/2013
d) 1120
View Answer
Explanation: In this program, We are using the setw function to print the date in correct format.
Output:
$ g++ form.cpp $ a.out 01/01/2013
9. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
unsigned long x = 64;
cout << x << oct << x << endl;
return 0;
}
a) 64100
b) 48
c) 345
d) 496
View Answer
Explanation: In this program, We are finding the octal number of given number by using oct function.
Output:
$ g++ form1.cpp $ a.out 64100
10. What is the use of the function "showbase"?
a) Indicate the base used
b) Indicate the variable
c) Indicate the base used & variable
d) Indicate the derived
View Answer
Explanation: showbase is used to indicate the base used.
Sanfoundry Global Education & Learning Series - C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for Computer Science Internship
- Practice Programming MCQs
- Check C++ Books
- Practice Computer Science MCQs
- Check Programming Books