C How To Program, 8th Edition By Deitel & deitel Test Bank

<< Calculation of Drug Dosages A Work Text 9th Edition by Sheila J. Ogden Test Bank Community & Public Health Nursing Promoting the Public Health 8th ed by Allender , Judith Test Bank >>
Product Code: 222
Availability: In Stock
Price: $24.99
Qty:     - OR -   Add to Wish List
Add to Compare

C How To Program, 8th Edition By Deitel & deitel Test Bank

Description

WITH ANSWERS
C How To Program, 8th Edition By Deitel & deitel Test Bank

2.1  Introduction

(No questions.)

2.2  A Simple C Program: Printing a Line of Text

2.1  Which of the following must every C program have?

(a)  main

(b)  #include

(c)  /*

(d)  

ANS: (a)

 

2.2  Every statement in C must end with a

(a)  period (.)

(b)  semicolon (;)

(c)  colon (:)

(d)  backslash (/)

ANS: (b)

 

2.3  Which of the following is not a valid escape sequence?

(a) 

(b)  \

(c)  \~

(d)  \

ANS: (c)

 

2.4  Which statement about comments is false?

  1. a) Comments begin and end with /* and */, respectively.
  2. b) Programmers insert comments to document programs and improve program readability.
  3. c) Comments do not cause any machine language object code to be generated.
  4. d) Lengthy comments can cause poor execution-time performance.

ANS: (d)

 

2.5  Lines beginning with a # are processed

  1. a) at execution time.
  2. b) at compile time.
  3. c) at preprocessor time.
  4. d) at postprocessor time.

ANS: (c)

 

2.6  Which of the following statements about the inclusion of is false?

  1. a) It is required.
  2. b) This header file contains information and declarations used by the compiler when compiling standard input/output library functions such as printf.
  3. c) This header file contains information that helps the compiler determine if calls to library functions have been made correctly.
  4. d) This header helps locate bugs in your program at compile time, rather than at execution time (when errors are usually more costly to correct).

ANS: (a)

 

2.7  In the line

 

int main()

 

the parentheses indicate that main is a program building block called a

  1. a) module
  2. b) statement
  3. c) directive
  4. d) function

ANS: (d)

 

2.8  The pair of braces that delineate the body of main and the portion of the program between these braces is called a __________.

  1. a) function
  2. b) block
  3. c) statement
  4. d) header

ANS: (b)

 

2.9  Which of the following is not a synonym for a C string?

  1. a) message
  2. b) character string
  3. c) character
  4. d) literal

ANS: (c)

 

2.10  The following line is most properly an example of a __________.

 

puts( Welcome to C! );

 

  1. a) function
  2. b) block
  3. c) statement
  4. d) header

ANS: (c)

 

2.11  In a printf, a backslash is printed by enclosing in quotes

  1. a) \
  2. b) \
  3. c) /\
  4. d) //

ANS: (b)

 

2.12  A linked program is often called a(n) __________.

  1. a) chain
  2. b) library
  3. c) object
  4. d) executable

ANS: (d)

 

2.13  The escape sequence for horizontal tab is __________.

  1. a) ab
  2. b)
  3. c) \horizontaltab
  4. d) \T

ANS: (b)

 

2.3  Another Simple C Program: Adding Two Integers

 

2.14  Which of the following is not a valid integer value?

(a)  -3

(b)  0

(c)  2134859

(d)  1.1

ANS: (d)

 

 

2.15  Which of the following is an invalid identifier (variable name)?

(a)  _Test

(b)  TEST

(c)  5test

(d)  test1

ANS: (c)

 

2.16  Which statement prints hi on the screen?

(a)  puts(hi);

(b)  put hi;

(c)  puts hi;

(d)  none of the above

ANS:  (a)

 

2.17  The __________ sign is also known as the __________ operator.

(a)  +, assignment

(b)  =, assignment

(c)  *, stream manipulator

(d)  &, stream insertion

ANS: (b)

 

2.18  A(n) __________ is a location in the computers memory where a value can be stored for use by a program.

  1. a) unknown
  2. b) name
  3. c) variable
  4. d) declaration

ANS: (c)

 

2.19  Which statement is false.

  1. a) Variables may be defined anywhere in the body of main.
  2. b) All variables must be defined before they are used.
  3. c) All variable definitions must include the name and data type of each variable.
  4. d) Several variables of the same data type may be defined in one definition.

ANS: (a)

 

2.20  Which of these is not a valid identifier?

  1. a) a_valid_identifier
  2. b) a1_valid_identifier
  3. c) a_valid_identifier_
  4. d) 1_valid_identifier

ANS: (d)

 

2.21  Which of the following statements is false?

  1. a) C is case sensitive.
  2. b) Uppercase and lowercase letters are different in C.
  3. c) identifier and IdEnTiFiEr are identical identifiers in C.
  4. d) Identifiers can be of any length

ANS: (c)

 

2.22  Which of the following multiple word variable names does not conform to the good programming practices in the text?

  1. a) multiple_word_variable_name
  2. b) multipleWordVariableName
  3. c) multiplewordvariablename
  4. d) aReallyReallyLongMultipleWordVa

ANS: (c)

 

2.23  The address operator is

  1. a) &&
  2. b) %
  3. c) @
  4. d) &

ANS: (d)

 

2.24  Which statement is false?

  1. a) in the statement

 

sum = integer1 + integer2;

 

both = and + are binary operators.

  1. b) The statement in part a) is an example of an assignment statement.
  2. c) The spaces around each of the binary operators in the statement of part a) are required.
  3. d) In part a), the = operators two operands are sum and the value of the expression integer1 + integer2.

ANS: (c)

 

2.25  Which of the following is false?

  1. a) Each variable being input in a scanf statement is generally preceded by an &.
  2. b) Each variable being output in a printf statement is generally not preceded by an &.
  3. c) In a printf statement, the comma that separates the format control string from the expressions to be printed is placed inside the format control string.
  4. d) Calculations can be performed inside printf

ANS: (c)

2.4  Memory Concepts

 

2.26  Variable names actually correspond to __________.

(a)  locations in the computers memory

(b)  operators

(c)  integers

(d)  data types

ANS: (a)

 

2.27  When a number gets assigned to a variable that already has a value __________.

(a)  the new number overwrites the previous value at that memory location

(b)  the new number gets assigned to a neighboring memory location

(c)  the computer issues an error

(d)  the new value is destroyed and the old value remains

ANS: (a)

 

2.28  Every variable has all the attributes below, except

  1. a) name
  2. b) value
  3. c) alias
  4. d) type

ANS: (c)

 

2.29  Which of the following is false?

  1. a) Reading a value into a memory location destroys the previous value.
  2. b) Reading a value out of a memory location destroys that value.
  3. c) sum = integer1 + integer2; involves destructive read-in.
  4. d) The statement in part c) also involves nondestructive read-out.

ANS: (b)

2.5  Arithmetic in C

 

2.30  Which operation will find the remainder when 15 is divided by 6?

(a)  15 / 6

(b)  15 % 6

(c)  15 ^ 6

(d)  15 * 6

ANS: (b)

 

2.31  Evaluate the expression

3 * 4 % 6 + 4 * 5

(a)  20

(b)  26

(c)  12

(d)  32

ANS:  (a)

 

2.32  Which statement is false?

  1. a) In algebra, we write ab to multiply a times b.
  2. b) In C, we write ab to multiply a times b.
  3. c) In C, the remainder operator is %.
  4. d) In C, integer division yields an integer result.

ANS: (b)

 

2.33  Which statement about C arithmetic is false?

  1. a) 6 / 3 yields 2
  2. b) 5 / 2 yields 2.5
  3. c) 7 % 3 yields 1
  4. d) 6 % 3 yields 0

ANS: (b)

 

2.34  a * ( b + c ) may also be written in C as

  1. a) ab + ac
  2. b) (a * b ) + c
  3. c) a * b + c
  4. d) a * b + a * c

ANS: (d)

 

2.35  Which statement about precedence is false?

  1. a) Parentheses may be used to force the order of evaluation to occur in any sequence desired by the programmer.
  2. b) Nested, or embedded parentheses are evaluated last.
  3. c) Multiplication has a higher precedence than addition.
  4. d) Subtraction has a lower precedence than division.

ANS: (b)

 

2.36  Which expression is true?

  1. a) The expression a * (b + c) + c * (d + e) contains nested parentheses.
  2. b) The expression y = a * x * x + b * x + c does exponentiation without an exponentiation operator.
  3. c) The C standard library provides function power to perform exponentiation.
  4. d) When we say evaluation of an expression proceeds from left to right we are referring to the additivity of the operators.

ANS: (b)

2.6  Decision Making: Equality and Relational Operators

 

2.37  Cs if statement executes the statement inside its body if a specified __________ is __________.

(a)  condition, true

(b)  condition, false

(c)  equality operator, true

(d)  relational operator, true

ANS:  (a)

 

2.38  Which of the following is an equality operator?

(a)  ==

(b)  =

(c)  >

(d)  >=

ANS: (a)

 

2.39  Which statement is false?

  1. a) Executable C statements either perform actions or make decisions.
  2. b) If the condition in an if statement is met, the statement in the body of the if statement is executed.
  3. c) All the relational operators have the same level of precedence.
  4. d) The equality operators have a higher level of precedence than the relational operators.

ANS: (d)

 

2.40  Which statement is false?

  1. a) Whitespace characters such as tabs, newlines and spaces are generally ignored by the C compiler.
  2. b) The statements in an if statement must be indented.
  3. c) Placing a blank line before and after every control structure can improve program readability.
  4. d) There can be (but should not be) more than one statement per line.

ANS: (b)

 

2.41  Which statement is false?

  1. a) It is not correct to split an identifier with a space, a tab or a newline.
  2. b) Statements and comments may be split over several lines.
  3. c) The equals sign (=) is not an operator.
  4. d) A good programming practice is to break a line after a comma in a lengthy comma-separated list.

ANS: (c)

 

2.42  Which of the following is not a keyword?

  1. a) int
  2. b) return
  3. c) if
  4. d) main

ANS: (d)

 

2.43  Which statement is false?

  1. a) The assignment operator associates from left to right.
  2. b) The arithmetic operators associate from left to right.
  3. c) The equality operators associate from left to right.
  4. d) The relational operators associate from left to right.

ANS: (a)

 

2.44  The order in which statements are __________ is called flow of control.

  1. a) entered in a source file
  2. b) preprocessed
  3. c) compiled
  4. d) executed

ANS: (d)

2.7  Secure C Programming

2.45  Which of the following statements is true in secure C programming?

(a)  You should avoid using printf to display a single string argument.

(b)  You should always use printf to display a single string argument.

(c)  You should always use puts to display a single string argument.

(d)  None of the above.

ANS: (a)

 

2.46  Which of the following statements should be used in secure C programming to display the string Welcome not followed by a newline character?

(a)  printf( Welcome );

(b)  puts( Welcome );

(c)  printf( %s, Welcome );

(d)  None of the above.

ANS:  (c)

12.1 Introduction

 

 

12.1 __________ are collections of data items lined up in a rowinsertions and deletions are made anywhere in a __________.

  1. a) Linked lists, linked list
  2. b) Queues, queue
  3. c) Stacks, stack
  4. d) Binary trees, binary tree

ANS: (a)

 

12.2 __________ are important in compilers and operating systemsinsertions and deletions are made only at one end of a __________its top.

  1. a) Linked lists, linked list
  2. b) Queues, queue
  3. c) Stacks, stack
  4. d) Binary trees, binary tree

ANS: (c)

 

12.3 __________ represent waiting lines; insertions are made at the back (also called the tail) and deletions are made from the front (also called the head) of a __________.

  1. a) Linked lists, linked list
  2. b) Queues, queue
  3. c) Stacks, stack
  4. d) Binary trees, binary tree

ANS: (b)

 

12.4 __________ facilitate high-speed searching and sorting of data, efficient elimination of duplicate items and compiling expressions into machine language.

  1. a) Linked lists
  2. b) Queues
  3. c) Stacks
  4. d) Binary Trees

ANS: (d)

 

12.2 Self-Referential Structures

 

12.5 A self-referential structure contains a ________ member that points to ________.

(a) integer, a structure of the same structure type

(b) pointer, an integer

(c) integer, an integer

(d) pointer, a structure of the same structure type

ANS: (d)

 

12.6 A(n) __________ pointer normally indicates the end of a data structure.

  1. a) uninitialized
  2. b) NULL
  3. c) self
  4. d) dereferenced

ANS: (b)

 

12.3 Dynamic Memory Allocation

 

12.7 A __________ occurs when dynamically allocated memory is not returned when its no longer needed.

(a) memory leak

(b) self-referential error

(c) allocation error

(d) sizeof error

ANS: (a)

 

 

12.8 __________ memory allocation is the ability for a program to obtain more memory space at execution time and to release space no longer needed.

  1. a) Static
  2. b) Active
  3. c) Selective
  4. d) Dynamic

ANS: (d)

 

12.9 Function malloc takes as an argument the number of bytes to be allocated, and returns a pointer of type __________ to the allocated memory.

  1. a) char *
  2. b) int *
  3. c) void *
  4. d) NULL *

ANS: (c)

 

12.10 If no memory is available malloc returns a(n) __________ pointer.

  1. a) self
  2. b) NULL
  3. c) void
  4. d) empty

ANS: (b)

 

12.11 Which of the following statements is true?

  1. a) A structures size is sometimes smaller than the total of the sizes of its members.
  2. b) A structures size is always larger than the total of the sizes of its members.
  3. c) A structures size is not necessarily the sum of the sizes of its members.
  4. d) A structures size is the sum of the sizes of its members.

ANS: (c)

 

12.12 Which is correct?

  1. a) Use the size operator to determine the size of a structure.
  2. b) Use the struct size operator to determine the size of a structure.
  3. c) Use the sizeof operator to determine the size of a structure.
  4. d) Determine the size of a structure manually by carefully adding up the sizes of the members.

ANS: (c)

 

12.13 Not returning dynamically allocated memory when it is no longer needed can cause a system to run out of memory prematurely. This is called a(n) __________.

  1. a) outage
  2. b) memory hole
  3. c) memory access violation
  4. d) memory leak

ANS: (d)

 

12.14 When memory allocated with malloc is no longer needed, return that memory to the system immediately with __________.

  1. a) free_memory
  2. b) free_storage
  3. c) return
  4. d) free

ANS: (d)

 

12.15 Which of these is not a common programming error?

  1. a) Referring to memory that has been freed.
  2. b) Freeing memory (with free) that was not dynamically allocated.
  3. c) Assuming that the size of a structure is simply the sum of the sizes of its members.
  4. d) Calling malloc in a statement without using sizeof.

ANS: (d)

 

12.4 Linked Lists

 

12.18 __________ is not an advantage of linked lists when compared to arrays.

(a) Dynamic memory allocation

(b) Efficient insertion and deletion

(c) Direct access to any list element

(d) Efficient use of memory

ANS: (c)

 

12.19 For a non-empty linked list, select the code that should appear in a function that adds a node to the end of the list. newPtr is a pointer to the new node to be added, and lastPtr is a pointer to the current last node. Each node contains a pointer nextPtr, a link to a node.

(a)
lastPtr->nextPtr = newPtr;

lastPtr = newPtr;

(b)
lastPtr = newPtr;

lastPtr->nextPtr = newPtr;

(c)
newPtr->nextPtr = lastPtr;

lastPtr = newPtr;

(d)
lastPtr = newPtr;

newPtr->nextPtr = lastPtr;

ANS: (a)

 

12.20 How many pointers are contained in a circular, doubly linked list with five nodes?

(a) 5

(b) 8

(c) 15

(d) 10

ANS: (d)

 

 

12.21 A linked list is a __________ collection of self-referential structures, called nodes, connected by pointer links.

  1. a) hierachical
  2. b) linear
  3. c) branching
  4. d) constant

ANS: (b)

 

12.22 Which of the following is a non-linear data structure?

  1. a) linked list
  2. b) queue
  3. c) binary tree
  4. d) stack

ANS: (c)

 

12.23 Which of the following is false?

  1. a) Lists of data can be stored in arrays.
  2. b) The length of a linked list can vary dynamically.
  3. c) Arrays can become full.
  4. d) Linked lists cannot become full.

ANS: (d)

 

12.24 Which of the following is false?

  1. a) Arrays can be maintained in sorted order.
  2. b) Linked lists can be maintained in sorted order.
  3. c) Insertion and deletion in a sorted array (while maintaining sorted order) is efficient.
  4. d) Once the insertion point or the node to be deleted has been located, insertion or deletion in a sorted linked list (while maintaining sorted order) is efficient.

ANS: (c)

 

12.25 Which statement is false?

  1. a) Arrays are normally stored contiguously in memory.
  2. b) Dynamic memory allocation can sometimes use memory more efficiently than using fixed-size data structures.
  3. c) Dynamic memory allocation incurs execution-time overhead.
  4. d) Linked lists are normally stored contiguously in memory.

ANS: (d)

 

12.26 Functions such as isEmpty and isFull that test a condition and return a value that can be interpreted as true or false, are called __________ functions.

  1. a) imperative
  2. b) declarative
  3. c) predicate
  4. d) conditional

ANS: (c)

 

12.27 Passing a pointer to a pointer is called __________.

  1. a) double direction
  2. b) pointer passing
  3. c) double indirection
  4. d) indirection

ANS: (c)

 

12.5 Stacks

 

12.28 Which of the following statements about stacks is incorrect?

(a) stacks can be implemented using linked lists.

(b) stacks are first in, first-out (FIFO) data structures.

(c) new nodes can only be added to the top of the stack.

(d) the last node (the bottom) of a stack has a null (zero) link.

ANS: (b)

 

12.29 A stack is initially empty, then the following commands are performed.

push 5

push 7

pop

push 10

push 5

pop

Which of the following is the correct stack (assume the top of the stack is on the left).

(a) 5 10 7 5

(b) 5 10

(c) 7 5

(d) 10 5

ANS: (d)

 

 

12.30 New nodes can be added to a stack and removed from the stack only at its top. For this reason a stack is referred to as a __________ data structure.

  1. a) first-in, first-out
  2. b) linear
  3. c) last-in, first-out
  4. d) dynamic

ANS: (c)

 

12.31 The link member in the last node of a stack is typically set to __________ indicate the bottom of the stack.

  1. a) void
  2. b) void *
  3. c) NULL
  4. d) empty

ANS: (c)

 

12.32 Which of the following statements is false?

  1. a) The primary functions used to manipulate a stack are push and pop.
  2. b) Function pop removes a node from the bottom of the stack.
  3. c) Function push creates a new node and places it on top of the stack.
  4. d) A stack can be implemented as a constrained version of a linked list by allowing insertions and deletions only at one end of the linked list.

ANS: (b)

 

12.33 Which is not a popular application of stacks?

  1. a) enabling called functions to return to their callers
  2. b) supporting recursive function calls
  3. c) containing the space created for automatic variables
  4. d) maintaining waiting lines

ANS: (d)

 

12.6 Queues

 

12.34 A queue receives the following commands (in pseudo-code):

enqueue 4, 6, 8, 3, 1

dequeue three elements

enqueue 3, 1, 5, 6

dequeue two elements

What number is at the front of the queue?

(a) 3

(b) 4

(c) 5

(d) 6

ANS: (a)

 

12.35 A linked list has the functions insertAtFront, removeFromFront, insertAtBack, and removeFromBack, which perform operations on nodes exactly as their names describe. Which two functions would most

naturally model the operation of a queue?

(a) insertAtBack and removeFromBack.

(b) insertAtBack and removeFromFront.

(c) insertAtFront and removeFromFront.

(d) insertAtFront and removeFromBack.

ANS: (b)

 

 

12.36 Queues are linear data structures with the property that queue nodes are inserted only at the tail of the queue and removed only from the head of the queue. For this reason, queues are referred to as __________ data structures.

  1. a) first-in, first-out
  2. b) first-in, last-out
  3. c) last-in, first-out
  4. d) first-come, first-served

ANS: (a)

 

12.37 Which of the following is not true of queues?

  1. a) Network packets wait in queues for service at routers .
  2. b) The entry at the front (or head) of the queue is the next to be removed.
  3. c) Queues are used to support print spooling.
  4. d) Queues are used to support high-speed sorting algorithms.

ANS: (d)

 

12.7 Trees

 

12.38 Select the incorrect statement. Binary trees (regardless of the order in which the values are inserted into the tree)

(a) always have multiple links per node.

(b) can be sorted efficiently.

(c) always have the same shape for a particular set of data.

(d) are nonlinear data structures.

ANS: (c)

 

12.39 Add the following nodes to a binary search tree in the order they appear.

6 34 17 19 16 10 23 3

What is the output of a postorder traversal of this tree?

(a) 3 10 16 23 19 17 34 6

(b) 3 6 17 16 10 19 23

(c) 6 3 34 17 16 10 19 23

(d) 10 16 23 19 17 34 3 6

ANS: (a)

 

12.40 Suppose you have a list of names sorted in alphabetical order, already stored in one of the data types below. The easiest way to print the names in reverse alphabetical order would be to use a

(a) binary search tree

(b) stack

(c) queue

(d) circular, singly linked list

ANS: (b)

 

12.41 If you have a 1000-element balanced binary search tree, what is the maximum number of comparisons that may be needed to find an element in the tree?

(a) 500

(b) 10

(c) 20

(d) 8

ANS: (b)

 

 

12.42 Which statement about trees is false?

  1. a) A tree is a non-linear, two-dimensional data structure.
  2. b) Tree nodes contain two or more links.
  3. c) Binary tree nodes contain two or fewer links.
  4. d) Binary tree nodes contain exactly two links.

ANS: (c)

 

12.43 Which statement is not true for binary trees.

  1. a) The left child in the root node is the first node in the left subtree.
  2. b) The children of a node are called siblings.
  3. c) A node with no children is called an orphan.
  4. d) The root node is the first node in the tree.

ANS: (c)

 

12.44 Which of the following statements about binary search trees with no duplicate values is false?

  1. a) The values in any left subtree are less than the values in its parent node.
  2. b) The values in any right subtree are less than the values in its parent node.
  3. c) The shape of the tree that corresponds to a particular set of data can vary based on the order in which the values are inserted into the tree.
  4. d) It is possible that a binary tree could contain all its values along one straight path through the tree.

ANS: (b)

 

12.45 A node can only be inserted __________ in a binary search tree.

  1. a) as the root node
  2. b) as a leaf node
  3. c) as a parent node
  4. d) as an ancestor node

ANS: (b)

 

12.46 The steps for an in-order traversal of a binary search tree include each of the following except _________.

  1. a) Traverse the left subtree in-order.
  2. b) Process the value in the root node.
  3. c) Skip over duplicate values.
  4. d) Traverse the right subtree in-order,

ANS: (c)

 

12.47 Which type of binary search tree traversal processes the node values in ascending order?

  1. a) in-order traversal
  2. b) pre-order traversal
  3. c) post-order traversal
  4. d) duplicate elimination traversal

ANS: (a)

 

12.48 Which of the following statements about binary search trees is false?

  1. a) The binary search tree facilitates duplicate elimination.
  2. b) In a tightly packed binary search tree, each level contains about half as many elements as the previous level. (The previous level is the level closer to the root node.)
  3. c) When searching a tightly packed billion-element search tree, only about 30 elements (or fewer) are required to locate most elements.
  4. d) When searching a tightly packed million-element search tree, only about 20 elements (or fewer) are required to locate most elements.

ANS: (b)

 

12.49 Which statement about the level-order traversal of a binary tree is false?

  1. a) It visits the nodes of a tree row by row.
  2. b) The search begins at the root node.
  3. c) The search begins at the row of the leftmost leaf node.
  4. d) On each level of the tree, the nodes are visited left to right.

ANS: (c)

 

12.8 Secure C Programming

 

12.50 Which of the following statements is false?

(a) Pointers should not be left uninitialized.

(b) When you use free to deallocate dynamically allocated memory, the pointer passed to free is set to NULL.

(c) Undefined behavior occurs when you attempt to use free to deallocate dynamic memory that was already deallocated

(d) Function malloc returns NULL if its unable to allocate the requested memory.

ANS: (b)

 

 

Chapter 22: Exception Handling: A Deeper Look

 

Section 22.1 Introduction

 

22.1 Q1: Exception handling may allow a program to:

  1. Terminate in a controlled manner.
  2. Be more robust and fault-tolerant.
  3. Continue executing as if no problem was encountered.
  4. All of the above.

ANS d. All of the above.

 

 

Section 22.2 Example: Handling an Attempt to Divide by Zero

 

22.2 Q1: The correct order in which an exception is detected and handled is:

  1. try, catch, throw
  2. throw, catch, try
  3. catch, throw, try
  4. try, throw, catch

ANS d. try, throw, catch

 

22.2 Q2: Once an exception is thrown, when can control return to the throw point?

  1. Only after the exception is caught.
  2. Once the stack unwinding process is completed.
  3. Immediately after the exception is thrown.

ANS a. Never.

 

22.2 Q3: The try block cannot:

  1. Enclose the code that may throw the exception.
  2. Enclose its own catch blocks.
  3. Test enclosing try blocks for additional catch statements if this try blocks catch statements cant match the exception being thrown.
  4. Have exceptions explicitly or implicitly thrown in the try block itself.

ANS b. Enclose its own catch blocks.

 

22.2 Q4: catch blocks are not required to contain:

  1. Braces { }.
  2. Parentheses ( ).
  3. Some form of parameter type indication.
  4. A parameter name.

ANS d. A parameter name.

 

22.2 Q5: An exception:

  1. Terminatesprogram execution.
  2. Terminates the block where the exception occurred.
  3. Will terminate the block where the exception occurred unless a catch command stops it.
  4. Will not terminate a block unless explicitly instructed to do so.

ANS b. Terminates the block where the exception occurred.

 

 

Section 22.3 Rethrowing an Exception

 

22.3 Q1: To rethrow an exception, the exception handler must:

  1. Use the throw; statement.
  2. Use the throw command with the same parameters as the original exception.
  3. Return a reference to whatever caused the original exception.
  4. Not have attempted to process that exception at all.

ANS a. Use the throw; statement.

 

22.3 Q2: Select the false statement.  A rethrown exception:

  1. Is detected by the next enclosing try block.
  2. Is the immediate result of a throw command.
  3. Can be processed by exception handlers following the enclosing try block.
  4. Must have been fully processed at the time it was rethrown.

ANS d. Must have been fully processed at the time it was rethrown.

 

Section 22.4 Stack Unwinding

 

22.4 Q1: The purpose of stack unwinding is to:

  1. Attempt to catch exceptions that are not caught in their scope.
  2. Improve catch blocks by allowing them to handle multiple exceptions.
  3. Return control to the function that created the exception.
  4. Aid the terminate command in shutting down the program.

ANS: a. Attempt to catch exceptions that are not caught in their scope.

 

 

Section 22.5 When to Use Exception Handling

 

22.5 Q1: Exception handling should not be used:

  1. As an alternative for program control.
  2. To make error handling uniform on large projects.
  3. To deal with errors that do not arise very often.
  4. To deal with errors for components that will be widely used in other applications, such as classes and libraries.

ANS a. As an alternative for program control.

 

 

 

 

Section 22.6 Constructors, Destructors and Exception Handling

 

22.6 Q1: Select the false statement.  If an exception is thrown from a constructor:

  1. The object being constructed will not be constructed.
  2. For an array, destructors for all array elements are called, even if those array elements have not yet been constructed.
  3. The exception can contain the error information that the constructor would not be able to return in the normal manner.
  4. For an object with member objects, and whose outer object has not been constructed, the destructor is called for the member objects.

ANS: b. For an array, destructors for all array elements are called, even if those array elements have not yet been constructed.

 

 

Section 22.7 Exceptions and Inheritance

 

22.7 Q1: An advantage of using inheritance with exceptions is:

  1. The ability to catch related errors easily.
  2. Allowing catch statements to be imported into classes.
  3. The ability to explicitly test for derived class objects individually.
  4. The simplification of destructor calls for objects.

ANS: a. The ability to catch related errors easily.

 

 

Section 22.8 Processing new Failures

 

22.8 Q1: Select the false statement.  Depending on the compiler:

  1. A failed new operation can return a 0.
  2. A failed new operation can throw a bad_alloc
  3. A failed new operation can throw an exception if the header file has been included.
  4. A failed new operation can automatically be caught at compile time.

ANS: d. A failed new operation can automatically be caught at compile time.

 

22.8 Q2: Select the false statement.  The new operator:

  1. Can attempt to allocate as much memory as the programmer requests.
  2. Returns a pointer to a location in memory.
  3. Can indicate failure differently on different compilers.
  4. Throws a bad_alloc exception regardless of what function is registered with set_new_handler.

ANS: d. Throws a bad_alloc exception regardless of what function is registered with set_new_handler.  

 

 

Section 22.9 Class unique_ptr and Dynamic Memory Allocation

 

22.9 Q1: If dynamic memory has been allocated for an object and an exception occurs, then:

  1. The catch block will not work properly.
  2. A memory leak could result.
  3. The objects constructor will cause another exception.
  4. Multiple pointers to memory could be created.

ANS: b. A memory leak could result.

 

22.9 Q2: Which statement about class unique_ptr (of the new C++ standard) and dynamic memory allocation is false?

  1. An object of class unique_ptr maintains a pointer to dynamically allocated memory.
  2. When a unique_ptr object destructor is called (for example, when a unique_ptr object goes out of scope), it performs a destroy operation on its pointer data member.
  3. Class template unique_ptr provides overloaded operators * and -> so that a unique_ptr object can be used just as a regular pointer variable is.
  4. Class unique_ptr is part of the new C++ standard and it replaces the deprecated auto_ptr class.

ANS: b. When a unique_ptr object destructor is called (for example, when a unique_ptr object goes out of scope), it performs a destroy operation on its pointer data member. [The operation is delete.]

 

 

Section 22.10 Standard Library Exception Hierarchy

 

22.10 Q1: Select the false statement regarding exceptions.

  1. The C++ standard has a hierarchy of exception classes.
  2. All exception classes are accessible via .
  3. Several classes derive from class exception.
  4. The what function can be overridden in each class derived from exception.

ANS: b. All exception classes are accessible via .

 

22.10 Q2: Which class indicates that an error occurred in which an arithmetic result was larger than the largest number that can be stored in the computer?

  1. invalid_argument.
  2. bad_exception.
  3. out_of_range.
  4. overflow_error.

ANS: d. overflow_error.

 

 

 

Write a review

Your Name:


Your Review: Note: HTML is not translated!

Rating: Bad           Good

Enter the code in the box below:



 

Once the order is placed, the order will be delivered to your email less than 24 hours, mostly within 4 hours. 

If you have questions, you can contact us here