Solutions to Chapter 0

0-0. Compile and run the Hello, world! program.

Answer: on my Mac, simply ” g++ main.cpp”, then ” ./a.out”

0-1. What does the following statement do?

3+4;

Answer: It is an expression, result is 7; the statement has no side effects.

0-2. Write a program that, when run, wirtes

This (“) is a quote, and this (\) is a backslash.

Answer: Just use string literals to represent ‘”‘ and ‘\’. the code should be like

std::cout << “This (\”) is a quote, and this (\\) is a backslash.”;

0-3. The string literal “\t” represents a tab character; different C++ implementations display tabs in different ways. Experiment with your implementation to learn how it treats tabs.

Answer: This question does not have a specific answer. On my Windows 7, one tab is as long as four spaces; On my Mac Lion, one tab is as long as three spaces. Therefore, it is recommended not to use TAB but use space instead, to avoid the potential problem with different length of TAB across different system.

0-4. Write a program that, when run, wirtes the Hello, world! program as its output.

Answer: Just remember to use string literals to represent “\”  or “‘”.

0-5. Is this a valid program? Why or why not?

#include <iostream>

int main() std::cout << “Hello, world!” << std::endl;

Answer: This is NOT a valid program. It will be a compiler error: “Expected function body after function declaration.

0-6. Is this a valid program? Why or why not ?

#include <iostream>

int main() {{{{{{ std::cout<<“Hello, world!”<<std::endl;}}}}}}

Answer: This is a valid program. The exterior bracer and others are different. the exterior is required for the function body. The other are just defining blocks.

0-7. What about this one?

int main()

{

/* This is a comment….

because it uses /* and */ as blah blah */

std::cout<<“Does this work?”<<std::endl;

return 0;

}

Answer: It does NOT work. the second line of comment, after the first ending comment tag

“*/”, “as blah blah..” will be recognised as code, and the compiler will give an error.

Here, the error is “Use of undeclared identifier ‘as’ “.

0-8. …and this one?

#include <iostream>

 

int main (int argc, const char * argv[])

{

 

// insert code here…

//This is a comment that extends over several lines

// by suing // at the begining of each line insted of using /*

// or */ to delimit comments.

std::cout << “Hello, World!\n”;

return 0;

}

Answer: It is correct. the ‘//’ will make the compiler take all things behind within the same line as comment.

0-9. What is the shortest valid program?

Answer: on my MacOS, I wrote a program with an empty main function, which doesn’t have a return statement, while the return type of main is int. But still there is no error.

0-10. Rewrite the Hello, world! program so that a new line occurs everywhere that whitespace is allowed in the program.

Answer:

#include <iostream>

 

int

main (

)

{

 

// insert code here…

std::cout

<<

“Hello, World!\n”;

return

0;

}

 

Advertisements

One thought on “Solutions to Chapter 0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s