Answers to Chapter 3 Review Questions


Chapter 3


How does abstraction help the programmer to cope with complexity?


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.


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


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.


What are the advantages of using encapsulation in software construction?


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.


Which two C# keywords are important for implementing encapsulation?


private and public.


What are the differences between a class and its objects?


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


What is the significance of the class interface?


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


How do you specify the beginning of a comment?


With two double forward slash characters //.


Why use comments if the compiler ignores them?


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


What are keywords and identifiers?


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.


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


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.


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


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


What are the essential parts of a variable?


Its identifier, its type, and its value.


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


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


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."


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"); 

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


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.


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


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


What is the advantage of declaring variables?


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.


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


By using an if statement.


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


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.


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


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

Answers to Chapter 3 Programming Exercises


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!"


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.


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


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


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. 

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 © 2008-2017.
If you may any questions please contact us: