Here is a listing of C++ questions and puzzles on “Formatting” along with answers, explanations and/or solutions:
1. Which is used for formatting purpose in c++?
a) Whitespace
b) Container
c) &
d) Vector
Answer: a
Clarification: 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
Answer: d
Clarification: 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
Answer: c
Clarification: By formatting your code, the user can easily understand it and can upgrade it without any complexity.
4. Choose the correct formatted code.
a)
coutb)
coutc) cout<<"Hai";
d) cout>>"Hai";
Answer: b
Clarification: 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;
Answer: a
Clarification: Variable initialization should be done with one space on left and right of equal operator.6. Choose the correct formatted code.
a)int main(){ coutb)
int main() { coutc)
int main() { coutd)
main() { cin>>5;View Answer
Answer: b
Clarification: 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
Answer: b
Clarification: 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#include 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
Answer: c
Clarification: 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 -
using namespace std;
-
int main()
-
{ -
unsigned long x = 64;
-
cout << x << oct << x << endl;
-
return 0;
-
}
a) 64100
b) 48
c) 345
d) 496
Answer: a
Clarification: In this program, We are finding the octal number of given number by using oct function.
Output:
$ g++ form1.cpp $ a.out 64100
