COSC 1315

Programming Fundamentals

 Practice Text #140

Variables and Constants

Revised: February 3, 2007
By Richard G. Baldwin

File Pfsg00140.htm
Practice Text Index


Welcome

The practice tests in this series were written specifically for the benefit of my students in COSC 1315, Fundamentals of Programming.  They consists of questions, answers, and explanations.  The questions are based on the material covered in my series of online lecture notes for the course.  Each practice test is keyed to a specific lecture.  This practice test is keyed to lecture #140 titled Variables and Constants.

Questions



140-1.  True or False.  The scope of a variable determines the set of program instructions in which the variable is accessible.

Answer and Explanation

140-2.  True or False.  A variable can be thought of as an unnamed pigeon hole in memory where data is stored.

Answer and Explanation

140-3.  True or False.  Although some programming languages such as Java and C# require variables to be declared before they are used, this is not a requirement in C++ and some other programming languages as well.

Answer and Explanation

140-4.  True or False.  The declaration of a variable consists of three parts:

Answer and Explanation

140-5. True or False.  The statement shown in Listing 140-5 is a valid variable declaration.

Listing 140-5.
int aVariable;

Answer and Explanation

140-6.  True or False.  In the variable declaration shown in Listing 140-6, the name of the variable is int, and the name of the type is aVariable.

Listing 140-6.
int aVariable;

Answer and Explanation

140-7.  True or False.  In some cases, the initial contents of a variable can be established when the variable is declared, as shown in Listing 140-7.

Listing 140-7.
int aVariable = 55;

Answer and Explanation

140-8.  True or False.  As is the case for modern programming languages such as Java and C#, C++ does not allow the use of global variables.

Answer and Explanation

140-9.  What output is produced by the program shown in Listing 140-9?

Listing 140-9.
#include <iostream>
using namespace std;

int globalVariable = 0;

class Variables01{ 
  public:
  static void classMain(){
    cout << "In classMain function.\n";
    cout << "globalVariable = " << globalVariable
                                         << endl;
    globalVariable = globalVariable + 1;
    cout << "globalVariable = " << globalVariable
                                         << endl;

    //Instantiate an object in dynamic memory
    // and save its reference in a pointer 
    // variable.
    Variables01* ptrToObject = new Variables01();

    //Invoke an instance function on the object.
    ptrToObject -> doSomething();
  }//End classMain function
  //-------------------------------------------//

  //An instance function of the Variables01 class
  void doSomething(){
    cout << "In doSomething function.\n";
    cout << "globalVariable = " << globalVariable
                                         << endl;
    globalVariable = globalVariable + 1;
    cout << "globalVariable = " << globalVariable
                                         << endl;
  }//end doSomething function
};//End Variables01 class
//---------------------------------------------//

int main(){
  cout << "In global main function.\n";
  cout << "globalVariable = " << globalVariable
                                         << endl;
  globalVariable = globalVariable + 1;
  cout << "globalVariable = " << globalVariable
                                         << endl;
  Variables01::classMain();
  return 0;
}//end main

Answer and Explanation

140-10.  True or False.  A global variable is a variable that is declared:

Answer and Explanation

140-11.  True or False.  A global variable is accessible by any code in any function (including global functions) in the program that knows the name of the variable.

Answer and Explanation

140-12.  What output is produced by the program shown in Listing 140-12?

Listing 140-12.
#include <iostream>
using namespace std;

class Variables02{
  public:
  //Declare a public static class variable.
  static int aStaticVariable;
};//End Variables02 class
//---------------------------------------------//

int main(){
  cout << Variables02::aStaticVariable << endl;
  Variables02::aStaticVariable = 6;
  cout << Variables02::aStaticVariable << endl;
  return 0;
}//end main

Answer and Explanation

140-13.  True or False.  A variable that is declared using the static keyword inside a class but not inside a function is often referred to as a class variable.

Answer and Explanation

140-14.  True or False.  A separate copy of each class variable exists for every object that is instantiated from the class.  Each copy belongs to a specific object.

Answer and Explanation

140-15.  True or False.  A public static (class) variable can be accessed without a requirement for an object instantiated from the class.

Answer and Explanation

140-16.  True or False.  The scope resolution operator (::) can be used in conjunction with the class name to access a static variable that is declared in the named class in the absence of an object.

Answer and Explanation

140-17.  True or False.  In C++, static variables cannot be initialized inside the class.

Answer and Explanation

140-18.  True or False.  In C++, you must use a global initializer to initialize static variables or they will not be in scope and will not be accessible.  The required syntax is as shown in Listing 140-18.

Listing 140-18.
type class_name::static_variable = value

Answer and Explanation

140-19.  True or False.  Once declared and globally initialized, a public or private static variable is accessible by all of the static and non-static functions defined in the same class.

A public static variable is accessible by any code in any function (including global functions) in the program that knows the name of the variable and the name of the class in which it is declared.

Answer and Explanation

140-20.  True or False.  An instance variable is a variable that is declared inside a class, outside of a function, and is not declared to be static.

Answer and Explanation

140-21.  What output is produced by the program shown in Listing 140-21?

Listing 140-21.
#include <iostream>
using namespace std;

class Variables03{
  public:
  int instanceVariable;

  static void classMain(){
    //Instantiate two objects in dynamic memory
    // and save their references in two pointer 
    // variables.
    Variables03* ptrToObj01 = new Variables03();
    Variables03* ptrToObj02 = new Variables03();

    ptrToObj01 -> instanceVariable = 3;
    ptrToObj02 -> instanceVariable = 10;

    ptrToObj01 -> displayInstanceVariable();
    ptrToObj02 -> displayInstanceVariable();
  }//End classMain function
  //-------------------------------------------//

  void displayInstanceVariable(){
    cout << 
         "In displayInstanceVariable function\n";
    cout << instanceVariable << endl;
  }//end displayInstanceVariable function
};//End Variables03 class
//---------------------------------------------//

int main(){
  Variables03::classMain();
  return 0;
}//end main

Answer and Explanation

140-22.  True or False.  An object is an instance of a class.

Answer and Explanation

140-23.  True or False.  Every object has its own copy of every instance variable that is declared in the class from which the object is instantiated.

Therefore, each instance variable is the property of one instance (object) of the class.  (Hence the name instance variable.)

Answer and Explanation

140-24.  True or False.  The scope of an instance variable (public or private) includes all of the code in all of the instance functions (non-static functions) that are defined in the same class.

Answer and Explanation

140-25.  True or False.  Static functions have direct access to all instance variables, regardless of the class in which they are declared.

Answer and Explanation

140-26.  True or False.  When objects are instantiated in dynamic memory, public instance variables are accessible to any code anywhere in the program that has access to a pointer variable containing the address of the object in dynamic memory to which the instance variable belongs.

Answer and Explanation

140-27.  True or False.  A local variable is a variable that is declared outside of any block of code outside of any function.

Answer and Explanation

140-28.  What output is produced by the program shown in Listing 140-28?

Listing 140-28.
#include <iostream>
using namespace std;

class Variables04{ 
  public:
  static void classMain(){
    Variables04* ptrToObject = new Variables04();
    ptrToObject -> doSomething();
  }//End classMain function
  //-------------------------------------------//

  //An instance function of the Variables04 class
  void doSomething(){
    cout << localVariable << endl;
    int localVariable = 6;
  }//end doSomething function
};//End Variables04 class
//---------------------------------------------//

int main(){
  Variables04::classMain();
  return 0;
}//end main

Answer and Explanation

140-29.  True or False.  In the case where a variable is declared within a block that is nested within another block, the scope of the variable ends at the end of the nested block in which it is declared.

Answer and Explanation

140-30.  What output is produced by the program shown in Listing 140-30?  (Note that the actual addresses may change from one run to the next.)

Listing 140-30.
#include <iostream>
using namespace std;

class Variables05{ 
  public:
  static void classMain(){
    Variables05* ptrToObject = new Variables05();
    cout << "Object's address = " << ptrToObject 
                                         << endl;
    ptrToObject -> doSomething();
  }//End classMain function
  //-------------------------------------------//

  //An instance function of the Variables05 class
  void doSomething(){
    int var = 0;
    cout << "Local variable's contents = " 
                                  << var << endl;
    //Declare a pointer variable and store the
    // address of the local variable in it.
    int* ptrToVar = *var;
    *ptrToVar = 6;
    cout << "Local variable's address = " 
                             << ptrToVar << endl;
    cout << "Local variable's contents = " 
                            << *ptrToVar << endl;
    cout << "Local variable's contents = " 
                                  << var << endl;
  }//end doSomething function
};//End Variables05 class
//---------------------------------------------//

int main(){
  Variables05::classMain();
  return 0;
}//end main

Answer and Explanation

140-31.  What output is produced by the program shown in Listing 140-31?

Listing 140-31.
#include <iostream>
using namespace std;

class Variables06{
  public:
  static void classMain(){
    //Instantiate an object in dynamic memory
    // and save its reference in a pointer 
    // variable.
    Variables06* ptrToObject = new Variables06();

    //Invoke an instance function on the object.
    ptrToObject -> doSomething();
  }//End classMain function
  //-------------------------------------------//

  //An instance function of the Variables06 class
  void doSomething(){
    //Declare and initialize a constant.
    const double pi = 3.14159;

    //The following attempt to change the value
    // of a constant results in a compiler error.
    pi = 16.5;

    cout << pi << endl;
  }//end doSomething function
};//End Variables06 class
//---------------------------------------------//

int main(){
  Variables06::classMain();
  return 0;
}//end main

Answer and Explanation



Copyright 2007, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Baldwin@DickBaldwin.com


Answers and Explanations

Answer 31

A.  Compiler Error

Back to Question 31

Explanation 31

Can't change the value of a constant.

Answer 30

A.  Compiler Error

Back to Question 30

Explanation 30

Incorrect use of * in place of &

Answer 29

True

Back to Question 29

Explanation 29

Answer 28

A.  Compiler Error

Back to Question 28

Explanation 28

Variable is out of scope.

Answer 27

False

Back to Question 27

Explanation 27

Answer 26

True

Back to Question 26

Explanation 26

Answer 25

False

Back to Question 25

Explanation 25

Answer 24

True

Back to Question 24

Explanation 24

Answer 23

True

Back to Question 23

Explanation 23

Answer 22

True

Back to Question 22

Explanation 22

Answer 21

Back to Question 21

Explanation 21

Answer 20

True

Back to Question 20

Explanation 20

Answer 19

True

Back to Question 19

Explanation 19

Answer 18

True

Back to Question 18

Explanation 18

Answer 17

True

Back to Question 17

Explanation 17

Answer 16

True

Back to Question 16

Explanation 16

Answer 15

True

Back to Question 15

Explanation 15

Answer 14

False

Back to Question 14

Explanation 14

Answer 13

True

Back to Question 13

Explanation 13

Answer 12

A.  Compiler Error

Back to Question 12

Explanation 12

In C++, you must initialize static variables or they will not be in scope and will not be accessible.

Answer 11

True

Back to Question 11

Explanation 11

Answer 10

False

Back to Question 10

Explanation 10

Answer 9

Back to Question 9

Explanation 9

Answer 8

False

Back to Question 8

Explanation 8

Answer 7

True

Back to Question 7

Explanation 7

Answer 6

False

Back to Question 6

Explanation 6

Answer 5

True

Back to Question 5

Explanation 5

Answer 4

False

Back to Question 4

Explanation 4

Answer 3

False

Back to Question 3

Explanation 3

Answer 2

False

Back to Question 2

Explanation 2

Answer 1

True

Back to Question 1

Explanation 1



Copyright 2007, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Baldwin@DickBaldwin.com

-end-