Answers to Chapter 3 Review Questions

   


Chapter 3

1:

How does abstraction help the programmer to cope with complexity?

A:

Abstraction allows programmers to simplify the problem domain they are attempting to turn into a computer program and allows them to focus only on its relevant parts.

2:

Is the idea behind encapsulation confined to software design? Give an example from everyday life.

A:

No, the idea behind encapsulation is not confined to software design. Example: Even though a car engine can be adjusted in numerous ways, the driver can only regulate it in a few simple areas; the rest are encapsulated from the driver and are either preset from the factory or controlled by on-board computers. If the driver could make all possible adjustments, it would lead to two problems. It would be complicated to drive a car, and the likelihood of the driver harming the engine would increase manifold.

3:

What are the advantages of using encapsulation in software construction?

A:

Encapsulation allows an object to expose only the parts that are relevant to the outside world. This prevents outside users from tampering with the internal parts of an object. At the same time, it becomes simpler to operate the object because irrelevant complicating parts are not part of the object's interface.

4:

Which two C# keywords are important for implementing encapsulation?

A:

private and public.

5:

What are the differences between a class and its objects?

A:

A class is a passive blueprint (existing in the source code) for its active object counter parts.

6:

What is the significance of the class interface?

A:

The class interface is what the outside world uses to communicate with the objects of this class.

7:

How do you specify the beginning of a comment?

A:

With two double forward slash characters //.

8:

Why use comments if the compiler ignores them?

A:

People reading through the source code look at comments to understand the source code better.

9:

What are keywords and identifiers?

A:

A keyword has a distinct predefined meaning in the C# language. It is part of the C# vocabulary, just as "you" is part of the English vocabulary and has a special predefined meaning.

An identifier is another word for a name. Identifiers are used to identify C# elements, such as classes, methods, and variables, that must all have an associated identifier.

10:

How is a block specified in C#? What are blocks used for?

A:

A block is enclosed by the curly braces {}. It represents a logical unit in C#. For example, a block can belong to a class or a method.

11:

Can you write a program without a Main method? Why or why not?

A:

No, because the runtime always calls the Main method of a program to begin its execution.

12:

What are the essential parts of a variable?

A:

Its identifier, its type, and its value.

13:

What is a simple C# instruction called? How is it terminated?

A:

It is called a statement and must be terminated by a semicolon.

14:

Which class and which method can you call in the .NET Framework class library to print text on the console? Write a statement that prints "My dog is brown."

A:

The Write (or WriteLine) method of the System.Console class can be used to print text onscreen, as in the following:

 System.Console.WriteLine("My dog is brown"); 
15:

How is a method called? What happens when a method is called?

A:

By writing the method identifier and by providing the arguments the method requires. If the method is called from outside the object where it resides, the object name must also be specified.

When a method is invoked, the flow of execution enters the method body and the statements contained here are executed. Then execution returns to the caller.

16:

What is an assignment? Which symbol is used to perform an assignment?

A:

The mechanism of giving a new value to a variable is called assignment. The equals sign (=) is used to denote an assignment.

17:

What is the advantage of declaring variables?

A:

It ensures that the program only contains properly declared variables. This prevents stray variables, mistakenly created due to misspellings and the like, from appearing in a C# program.

18:

How can you make the program decide between two paths of execution?

A:

By using an if statement.

19:

What is whitespace? Does the compiler care much about whitespace?

A:

Whitespace is comprised of blank lines, space characters, tab characters, and carriage returns. The compiler ignores whitespace in most cases, but tokens cannot be broken up by whitespace.

20:

Do all programmers have to follow the same style to write valid C# programs?

A:

No, many different valid styles are applied in the programmer community.

Answers to Chapter 3 Programming Exercises

1:

Instead of printing "Bye Bye!" as the last text on the command console before the program finishes, change the source code so that the program writes "Bye Bye. Have a good day!"

2:

Instead of typing a y to have the program print "Hello World!", have the user type Yes. Have the program inform the user about this by changing line 10.

3:

Instead of using the variable name answer to hold the input from the user, change the name to userInput.

4:

Let the program print out an additional line under "Bye Bye. Have a good day!" saying "The program is terminating."

5:

Declare another variable of type string called userName. Before the program prints "Do you want me to write the famous words?", have the program request the user to type in his or her name. After the user has entered his or her name, have the program read this name and store it in the userName variable. Then have the program print "Hello" followed by the content of the username. Tip: the last printout can be accomplished by typing

 System.Console.WriteLine("Hello" + userName); 

A typical execution of the program should result in the following output:

 Please type your name Deborah<enter> Hello Deborah Do you want me to write the famous words? Type Yes for yes; n for no. Then <enter>. Yes<enter> Hello World! Bye Bye. Have a good day! The program is terminating. 
A:

The following listing provides answers to Exercises 1 5.

 // This is a simple C# program class Hello {      // The program begins with a call to Main()     public static void Main()     {         string userInput;         string userName;         System.Console.WriteLine("Please enter your name");         userName = System.Console.ReadLine();         System.Console.WriteLine("Hello " + userName);         System.Console.WriteLine("Do you want me to write the two famous words?");         System.Console.WriteLine("Type Yes or No. Then <enter>");         userInput = System.Console.ReadLine();         if (userInput == "Yes")             System.Console.WriteLine("Hello World!");         System.Console.WriteLine("Bye Bye. Have a good day");         System.Console.WriteLine("The program is terminating");     } } 


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net