Revised: February 3, 2007
By Richard G. Baldwin
File Pfsg00140.htm
Practice Text Index
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.
140-2. True or False. A variable can be thought of as an unnamed pigeon hole in memory where data is stored.
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.
140-4. True or False. The declaration of a variable consists of three parts:
140-5. True or False. The statement shown in Listing 140-5 is a valid variable declaration.
Listing 140-5.
int aVariable; |
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; |
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; |
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.
140-9. What output is produced by the program shown in Listing 140-9?
In global main function. globalVariable = 0 globalVariable = 1 In classMain function. globalVariable = 1 globalVariable = 2 In doSomething function. globalVariable = 2 globalVariable = 3
#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 |
140-10. True or False. A global variable is a variable that is declared:
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.
140-12. What output is produced by the program shown in Listing 140-12?
0 6
#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 |
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.
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.
140-15. True or False. A public static (class) variable can be accessed without a requirement for an object instantiated from the class.
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.
140-17. True or False. In C++, static variables cannot be initialized inside the class.
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 |
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.
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.
140-21. What output is produced by the program shown in Listing 140-21?
In displayInstanceVariable function 3 In displayInstanceVariable function 10
#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 |
140-22. True or False. An object is an instance of a class.
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.)
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.
140-25. True or False. Static functions have direct access to all instance variables, regardless of the class in which they are declared.
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.
140-27. True or False. A local variable is a variable that is declared outside of any block of code outside of any function.
140-28. What output is produced by the program shown in Listing 140-28?
6
#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 |
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.
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.)
Object's address = 0x3d3d50 Local variable's contents = 0 Local variable's address = 0x22ff34 Local variable's contents = 6 Local variable's contents = 6
#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 |
140-31. What output is produced by the program shown in Listing 140-31?
16.5
#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 |
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.
Can't change the value of a constant.
Incorrect use of * in place of &
Variable is out of scope.
In displayInstanceVariable function 3 In displayInstanceVariable function 10
In C++, you must initialize static variables or they will not be in scope and will not be accessible.
In global main function. globalVariable = 0 globalVariable = 1 In classMain function. globalVariable = 1 globalVariable = 2 In doSomething function. globalVariable = 2 globalVariable = 3
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.
-end-