Skip to main content

General Questions & Code related questions


  1. What is whitespace? 

  2. What is meant by freeform? 

  3. What does it mean to maintain programs? 

  4. What does C++ prefer most, uppercase or lowercase letters in programs? 

  5. What are comments? 

  6. Why are comments necessary? 

  7. What does the compiler do when it sees a comment? 

  8. What do all C++ comments begin with? 

  9. What is the difference between a C-style comment and a C++-style comment? 

  10. What happens when you nest C-style comments? 

  11. True or false: Longer programs are more difficult to understand than shorter ones. 

  12. True or false: You can substitute parentheses for braces if you feel that the parentheses are more readable. 

  13. True or false: Comments are not Visual C++ commands. 

  14. True or false: You can nest one C++ comment inside another. 

  15. Match the special character on the left with that special character's description on the right. 

    Special CharacterDescription
    [Backslash
    <Left bracket
    }Right-angled bracket
    |Right parenthesis
    \Forward slash (or just slash)
    ]Left-angled bracket
    {Left parenthesis
    )Right brace
    (Vertical line
    >Left brace
    /Right bracket


    There is no What's the Output? section in this unit. 


    Find the Bug


  16. Here is a comment and a C++ command (the return statement). Where is the problem? 


    // Go back to the IDE     return;

  17. The following program contains three comment bugs. See whether you can determine what is wrong. 

    // This program computes taxes  #include   void main()  {    The next few lines calculate payroll taxes    // Computes the gross pay   float gross = 40 * 5.25;    float taxes = gross * .40;   / Just computed the taxes    cout "The taxes are " <<>

    Write Code That. . .


  18. Tim Peterson wants to put his name at the top of his program using a comment. Write a comment that contains Tim's name using both the C++-style and the C-style approach. 

  19. Here is the same program you saw earlier, with one difference: The programmer forgot to precede the comments with double slashes. After trying to compile the program, the programmer looked at the 20 or so error messages and realized what was left out. See if you can help the programmer correct the program by inserting the proper commenting symbols everywhere they go. 

    Filename: 1STLONG.CPP  Longer C++ program that demonstrates comments,  variables, constants, and simple input/output  #include   void main()    {      int i, j;       These three lines declare four variables      char c;      float x;      i = 4;          i is assigned an integer literal      j = i + 7;      j is assigned the result of a computation      c = 'A';        Enclose all character literals                      in single quotation marks      x = 9.087;      x is a floating-point value      x = x * 12.3;   Overwrites what was in x with something else     Sends the values of the four variables to the screen      cout <<>

  20. Hint: Count the number of double slashes you put in this program. If you did not add 18 double slashes (or if you added more than 18), try again. 

    Extra Credit


  21. Of the following five lines, which contain useful comments and which contain redundant ones? 

      clog << '\n';                  // Sends an end-of-line to the error log  radius3 = radius1 + radius2;   // Calculates radius3                                 // The following code contains a C++ program                                 // The following code tracks your investments  clog << '\n';                  // Sends '\n' to clog

Comments

Popular posts from this blog

MFC - Microsoft Foundation Classes Design Patterns

1 Introduction This paper describes the use of object-oriented software design patterns, as presented in Design Patterns: Elements of Reusable Object-Oriented Software by Gamma et al., within the Microsoft Foundation Class Library (MFC). MFC is used for implementing applications for Microsoft Windows operating systems. Because of the size of the MFC library, a complete analysis would have been beyond the scope of this assignment. Instead, we identified various possible locations for design patterns, using the class hierachy diagram of MFC, and studied the source carefully at these locations. When we did not find a pattern where we expected one, we have documented it anyway, with examples of how the particular problem could have been solved differently, perhaps more elegantly, using design patterns. We have included a brief introduction to MFC in Section 2 , as background information. The analysis has been split into three parts, with one section for each major design pattern ca...

Explain Polymorphism and Flavors of Polymorphism...

Polymorphism is the ability of different objects to react in an individual manner to the same message. This notion was imported from natural languages. For example, the verb "to close" means different things when applied to different objects. Closing a door, closing a bank account, or closing a program's window are all different actions; their exact meaning is determined by the object on which the action is performed. Most object-oriented languages implement polymorphism only in the form of virtual functions. But C++ has two more mechanisms of static (meaning: compile-time) polymorphism: Operator overloading. Applying the += operator to integers or string objects, for example, is interpreted by each of these objects in an individual manner. Obviously, the underlying implementation of += differs in every type. Yet, intuitively, we can predict what results are. Templates. A vector of integers, for example, reacts differently from a vector of string objects when it receives ...

• Why might you need exception handling be used in the constructor when memory allocation is involved?

Your first reaction should be: "Never use memory allocation in the constructor." Create a separate initialization function to do the job. You cannot return from the constructor and this is the reason you may have to use exception handling mechanism to process the memory allocation errors. You should clean up whatever objects and memory allocations you have made prior to throwing the exception, but throwing an exception from constructor may be tricky, because memory has already been allocated and there is no simple way to clean up the memory within the constructor.