Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

QUESTION 1 The definition of a variable outside any function is called local var

ID: 3855337 • Letter: Q

Question

QUESTION 1

The definition of a variable outside any function is called

local variable definition

global variable definition

global function definition

global function header

QUESTION 2

A void function can have

exactly one argument

no more than 3 arguments

no arguments

as many arguments as the programmer wishes

QUESTION 3

Which of the following statements about the definition and declaration of functions is incorrect?

(Note. Function definition includes the body of the function also called function block)

A function header is exactly the same as function prototype except for the semicolon

Function definition is exactly the same as function prototype

A function definition is a function header followed by the function block

Function declaration is exactly the same as function prototype

QUESTION 4

Where can you NOT declare a variable in a C++ program?

Within the argument list of a function call

Within the parameter list of a function definition

Within the block of a void function

Within a block nested within another block

QUESTION 5

Here is a small program. Which of the statements about the variables is correct?

#include <iostream>

const double NUM = 2.9345358;

double num = 3;

double numTimes(int x);

int main( )

{

using namespace std;

int value;

cout << “Enter a value, I’ll multiply it by “

<< NUM << endl;

cin >> value;

cout << “You entered “ << value

<< “ NUM times this is “ << numTimes(value)

<< endl;

return 0;

}

double numTimes(int x)

{

double d;

d = NUM * x;

return d;

}

d is a local variable in the main function

value is a local variable in the main function

num is a global constant

NUM is a global variable

QUESTION 6

What do the calls to exit(...) do? When exit(0) and exit(1) are called, what receives these values and what is done with them?

the exit() function is obsolete. There is no longer any such function in the C++ libraries

the exit() function stops the program. The value is passed to the operating system which uses it for an error code

the exit() function stops the program. The value is discarded.

The exit() function temporarily stops the program, and sends the argument to the operating system which uses it for an error code. The operating system restarts the program after fixing the error.

QUESTION 7

Assume this code fragment is embedded in an otherwise correct and complete program. What should be the output from this code segment?

{

for( int i = 0; i < 10; i++)

{

     . . .

}

cout << i << endl;

}

The variable i is undefined in this scope, so this should not compile

9

10

0

QUESTION 8

Given the following include directive (to get the declaration for the pow function from the math library):

     #include

Now make these declarations:

     double base = 2, exponent = 3, power = 4;

Which of the following are correct invocations for the function pow?

power = pow(base, exponent);


base-1 = pow(exponent, power);

pow(power, base, exponent);

pow(base, exponent) = power;

QUESTION 9

Which statement is incorrect?

A variable declared outside any function is said to be a global variable

It is legal to replace the prototype

double totalCost(int numberParam, double priceParam);

with the more terse, alternate form

double totalCost(int, double);

Extensive use of global variables is NOT a satisfactory replacement for the difficulties of parameter passing with functions

In C++ Boolean value are represented only with the int values 0 for false and 1 for true

QUESTION 10

Which statement is incorrect?

Consider two blocks, one within another. If an identifier is declared as a variable in the innermost of these two blocks, one can access this variable from the outer block

A local variable is created in the execution stack

Consider two blocks, one within another. C++ allows an identifier to be declared as a variable in each of these blocks

A variable declared within a function block is said to be local to the function

QUESTION 11

A semicolon does not (usually) go after these with the exception of

a function header to make it a function declaration

if (condition)

int main()

while (condition)

QUESTION 12

Which of the following code fragments gives a random value between 2 and 5 inclusive? You can assume that the random number seed has been set and any needed definitions and initializations have been made

3.0*rand() + 2

rand()/static_cast (RAND_MAX)*2 +3

(RAND_MAX - rand())/static_cast<double>(RAND_MAX)*5-2

3.0*(RAND_MAX-rand())/RAND_MAX + 2

QUESTION 13

A call to a C++ function is

The name of the function followed by empty parentheses

The name of the function followed by a number of arguments not greater than the number of parameters in the definition

The name of the function followed by any number of arguments, regardless of the number of parameters in the definition

The name of the function followed by exactly the number of arguments as there are parameters in the definition

QUESTION 14

In C++ array indices, that is subscript values, must be

Less than or equal to the declared size of the array

positive

None is correct

Non-negative, integer

QUESTION 15

Which statement is incorrect?

If we want to find the median of 100 scores, we need 100 separate variables to hold the data

The programmer should always use a defined constant in an array declaration

With arrays, indices start with the number 0

An array behaves like a list of variables each of which is of the same type and for which there is a uniform, convenient naming convention that can be declared in a single line of code.

QUESTION 16

Which statement is incorrect?

To call a function with an array parameter, write the array argument as the array name followed by empty square brackets, as in f(a[], 7); (The first argument is an array a, the second is the size.)


Given the two C++ array declarations:

int a[10], b[10];

You can NOT assign b to a:

a = b;


Consider the array declaration, int x[20];. There is no memory allocated for x[20]

Arrays in C++ can store one type of data per array

QUESTION 17

What is the output of the following code (assuming it is embedded in a correct and complete program)?

char letter[5] = {'o', 'k', 'c', 'e', 'g'};

for(int i = 4; i >= 0; i-- )

cout << letter[i];

cout << endl;

ecko followed by a character from an out of bounds access

kceg followed by a character from an out of bounds access

okceg

gecko

QUESTION 18

Are the following array initializations INcorrect?

const int SIZE =4;

int x[SIZE];


int x[4] = {8, 7, 6, 5, 4};


int x[] = {8, 7, 6, 5, 4};


int x[4] = {8, 7, 6};

QUESTION 19

Which statement is incorrect?

Indexed variables for an array are stored wherever the computer finds memory for the first indexed variable, then the next one is stored next to the first if there is space, and someplace else if not.


If you need an array with more than one index, you can use a multidimensional array, which is actually an array of arrays

C++ arrays do not check for out-of-range index values

A for-loop is a convenient way to step through an array

QUESTION 20

Given the array declaration, int a[20]; The first element is written as:

a[20]

a

a[1]

a[0

local variable definition

global variable definition

global function definition

global function header

Explanation / Answer

(1)
   (a)   Variable declared outside of any function is called Global variable definition.
   (b)   Global variables are accessible to any function with in the program.
   (c) Global variables are generally defined at top of the function definitions
  
   Hence correct option is (B) global variable definition
  
___________________________________________________________________________________________________

(2)
   (a) void function is the function that doesn't return any value but there is no restriction on number of arguments it accepts.
   (b) Syntax: void <function_name>(<Parameter List>)
   (c) Parameters can be 0 or more but the function should not have any return value.

   Hence correct option is (D) as many arguments as the programmer wishes
  
___________________________________________________________________________________________________

(3)
   (a)   Function declaration(Prototype) specifies the function name, return type and type of parameters it accepts.
       Eg: void add(int, int);
  
   (b)   Function definition contains the body (set of executable statements).

   So the statement, "Function definition is exactly the same as function prototype" is in-correct.
  
   Hence correct option is (B)
  
___________________________________________________________________________________________________

(4)
   In C++, Variables can be declared at the following places:
      
       (a)   Within the parameter list of a function definition (eg. void multiply(int a, int b))
      
       (b) Within the block of a void function
           Eg:   void add() { int a, b, c; }
      
       (c)   Within a block nested within another block
      
   But Variable can't be declared Within the argument list of a function call.
  
   Hence correct option is (A)
  
___________________________________________________________________________________________________

(5)
   (A) d is a variable declared locally in the function numTimes. It is local to function numTimes but not to main function.
       Hence this statement is in-correct.
      
   (B)   value is a variable declared locally in the main function. It is local to function main but not to other functions.
       Hence this statement is correct.
      
   (C)   num is a global variable but not global constant.
       Hence this statement is in-correct.
      
   (D)   NUM is a global constant variable.
       Hence this statement is in-correct.
      
   Hence correct option is (B)
  
___________________________________________________________________________________________________

(6)
   (a)   Function exit() is used for terminating the program.
   (b)   Value is passed to the operating system which uses it for an error code.
   (c) exit(0) indicates the successful process execution, where as exit(1) indicates the error in process execution.
  
   Hence correct option is (B) the exit() function stops the program. The value is passed to the operating system which uses it for an error code
  
___________________________________________________________________________________________________

(7)
   (a) Variable i is declared with in the for loop. It's scope is restricted with in the loop. Outside the loop, i can't be accessed.
   (b)   Trying to access a variable outside its scope raises an error
  
   Hence correct option is (A) The variable i is undefined in this scope, so this should not compile
  
___________________________________________________________________________________________________

(8)
   (a) pow function of math library calculates the power value.
  
   (b) Syntax: double pow(double x, double y)
       Where x is the base and y is the exponent
       Calculates x power y
      
   (c) Eg: pow(5, 3) calculates 5 power 3 which is 125.
  
   Hence correct option is (A) power = pow(base, exponent);

___________________________________________________________________________________________________

(9)
   (A) A variable declared outside any function is said to be a global variable
   This Statement is correct
  
   (B)   Given replacement is legal.
   This statement is correct
  
   (C)   Extensive use of global variables is definitely satisfactory replacement for the difficulties of parameter passing with functions.
   This statement is not Correct
  
   (D)   In C++, Value 0 represents False and 1 represent True.
   This statement is correct
  
   Hence correct option is (C)
  
___________________________________________________________________________________________________

(10)
   (A) In the declaration of two blocks, one within another. If an identifier is declared as a variable in the innermost of these two blocks, one cannot access this variable from the outer block as its scope is pertained to corresponding block.
  
   Hence this statement is in-correct
  
   (B)   Local variables declared in the program are created in execution stack.
   Hence this is correct
  
   (C)   C++ allows an identifier to be declared as a variable in each of these blocks.
   Hence this is correct
  
   (D) Variable's declared with in the function are local to that function.
   Hence this is correct
  
   Hence correct option is (A)
  
___________________________________________________________________________________________________

(11)
   A semicolon (usually) go after with the exception of
  
   (A) A function header to make it a function declaration. (Eg. void add(int);)
   (B) if(condition). (Eg. if(a==b);)
   (D) while(condition). (Eg. while(a==b);)
  
   Hence correct option is (B) (Semicolon can't go after main)
  
___________________________________________________________________________________________________

(12)
  
   (1) Statement (RAND_MAX-rand())/static_cast<double>(RAND_MAX) generates a double value between 0.0 and 1.0.
   (2) Multiplying the above expression by 3 gives a value in the range 0.0 to 3.0
   (3) Adding value 2 gives a value in the range 2.0 to 5.0
  
   Hence correct answer is (D) 3.0*(RAND_MAX-rand())/RAND_MAX + 2

___________________________________________________________________________________________________

(13)
   A Call to C++ function is The name of the function followed by exactly the number of arguments as there are parameters in the definition.
  
   For eg:
   Definition: void add(int a, int b) { int c; c = a + b; }
  
   Function call should be add(23, 6)
  
   Hence correct option is (D)
___________________________________________________________________________________________________

(14)
  
   In C++ array indices, that is subscript values, must be Non-negative, integer.
  
   For eg: Array declaration int arr[10]; contains array indices from 0 to 9
  
   Hence correct option is (D)
___________________________________________________________________________________________________

(15)
   Arrays are used for storing more than one value of same data type. Its indices start from 0.
  
   So, Statements (B, C, D) are correct.
  
   For statement A, there is no need of creating 100 separate variables. Instead arrays can be used.
   So statement (A) is in-correct.
  
   Hence correct option is (A)
___________________________________________________________________________________________________

(16)
   (a)   To call a function with an array parameter, function call should include name of the array(no need of []) and size.
       So statement (A) is in-correct
  
   (b)   Arrays can't be assigned each other directly.
  
   (c)   int x[20]; allocates memory from x[0] to x[19]
  
   (d)   Arrays in C++ stores only elements of a same data type.
  
   Hence correct option is (A)  
___________________________________________________________________________________________________

(17)
   letter is char array with elements 'o', 'k', 'c', 'e', 'g'.
  
   For loop iterates from back to front (4 to 0 at a decrement of 1)
  
   Output of the program is gecko
  
   Hence correct option is (D)

___________________________________________________________________________________________________

(18)  
   Array initialization,
  
   int x[4] = {8, 7, 6, 5, 4}; is in-correct.
  
   x is declared to be of size 4 but assigning 5 elements. Hence raises an error.

Hence correct option is (B)
___________________________________________________________________________________________________

(19)
   Statement (A) is in-correct:
  
   Indexed variables for an array are stored wherever the computer finds memory for the first indexed variable, then the next one is stored next to the first place but not at some place.
  
  
   Hence correct option is (A)
___________________________________________________________________________________________________

(20)
   In array declaration, int a[20];
  
   Variable a is of size 20. Array indices start from 0 to 19.
  
   First element is written as: a[0]
  
   Hence correct option is (D)
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote