C++ Programming Questions and Answers – Formatting

This section on C++ questions and puzzles focuses on “Formatting”. One shall practice these questions and puzzles to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These programming puzzles can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

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
View Answer

Answer: a
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

Answer: d
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

Answer: c
Explanation: By formatting your code, the user can easily understand it and can upgrade it without any complexity.
advertisement
advertisement

4. Choose the correct formatted code.
a)

   cout << "Hai"
   << "Good morning";

b)

   cout << "Hai" <<
   "Good morning" << endl;
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

c) cout<<"Hai";
d) cout>>"Hai";
View Answer

Answer: b
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

Answer: a
Explanation: Variable initialization should be done with one space on left and right of equal operator.
advertisement

6. Choose the correct formatted code.
a)

   int main(){
   cout << "5";}

b)

   int main()
   {
   cout << "5";
   }
advertisement

c)

   int main()
   {
   cout << "5";}

d)

   main()
   {
   cin>>5;
View Answer
Answer: b
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

Answer: b
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?

  1.     #include <iostream>
  2.     #include <iomanip>
  3.     using namespace std;
  4.     void showDate(int m, int d, int y)
  5.     {
  6.         cout << setfill('0');
  7.         cout << setw(2) << m << '/'
  8.         << setw(2) << d << '/'
  9.         << setw(4) << y << endl;
  10.     }
  11.     int main()
  12.     {
  13.         showDate(1, 1, 2013);
  14.         return 0;
  15.     }

a) 1,1,2013
b) 2013
c) 01/01/2013
d) 1120
View Answer

Answer: c
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?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         unsigned long x = 64;
  6.         cout << x << oct << x << endl;
  7.         return 0;
  8.     }

a) 64100
b) 48
c) 345
d) 496
View Answer

Answer: a
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

Answer: a
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.