File nt1.html.pre read; 1266 lines!

Self Test 1

Topics:

  1. A program contains these two lines to read integer input values:

    	  int x, y, z, w;
    
    	  cin >> x >> y;
    	  cin >> z >> w;
    	

    The user types two input lines:

    	  5 10 15
    	 20 25 30
    	

    What values are read into z and w?

    1. 5 and 10
    2. 10 and 15
    3. 15 and 20
    4. 20 and 25
    5. 25 and 30
    next prev start
  2. Which of the following would NOT correctly open an input file named "infile.txt" located in the same directory as the program?

    1. #include <iostream>
      using namespace std;
      
      int main()
      {
        ifstream ifs;
      
        ifs.open("infile.txt");
        ...
      }
      	    
    2. #include <fstream>
      using namespace std;
      
      int main()
      {
        ifstream ifs;
      
        ifs = open("infile.txt");
        ...
      }
      	    
    3. #include <iostream>
      
      using namespace std;
      
      int main()
      {
        ifstream ifs;
      
        ifs.open("infile.txt");
        ...
      }
      	    
    4. #include <fstream>
      using namespace std;
      
      int main()
      {
        ifstream ifs;
      
        ifs.open("infile.txt");
        ...
      }
      	    
  3. Which of the following would output a double value x with 3 digits to the right of the decimal?

    	  double x = 1234.56789;
    	
    1. 	      cout.setf(ios::scientific);
      	      cout.precision(3);
      	      cout << x << endl;
      	    
    2. 	      cout.setf(ios::scientific);
      	      cout.precision(9);
      	      cout << x << endl;
      	    
    3. 	      cout.setf(ios::fixed);
      	      cout.precision(3);
      	      cout << x << endl;
      	    
    4. 	      cout.setf(ios::fixed);
      	      cout.precision(9);
      	      cout << x << endl;
      	    
  4. What is the output?

    	  #include <iostream>
    	  #include <iomanip>
    	  using namespace std;
    	  int main()
    	  {
    	    int x = 5;
    	    int y = 10;
    	    bool b = x > y;
    
    	    cout << boolalpha b << endl;
              }
    	
    1. 0
    2. 1
    3. 5
    4. false
    5. true
  5. Which of the following is a correct statement about functions f and g?

        1	double f(int x);
        2	int g(int x) {
        3	  if (x % 2 == 0) {
        4	    return x/2;
        5	  } else {
        6	    return 3*x + 1;
        7	  }
        8	}
        9	
       10	int main()
       11	{
       12	 double z = f(5);
       13	 int w = g(5);
       14	}	
    
    1. Line 12 is a compile error since f is not declared.
    2. Line 12 is a compile error since f is not defined
    3. Line 13 is a compile error since g is not declared.
    4. Line 13 is a compile error since g is not defined
    5. Neither line 12 nor line 13 is a compile error.
  6. Which assignment(s) will cause a compiler error?

        1	char x = 'a';
        2	int y = 5;
        3	float w = 2.5F;
        4	double z = 3.145;
        5	
        6	z = w;
        7	z = y;
        8	z = x;
        9	w = z;
       10	y = z;
       11	x = z;
    
    1. lines 9 - 11, only
    2. line 11 only
    3. line 10 and 11 only
    4. None will cause an error
  7. What are the values of a, b, and c after the call to f below?

    int f(int& x, int y)
    {
      x++;
      y++;
      return x + y;
    }
    int main()
    {
      int a = 5;
      int b = 10;
      int c = f(a,b);
    
    }
    
    1. a = 5, b = 10, c = 17
    2. a = 5, b = 11, c = 17
    3. a = 6, b = 10, c = 17
    4. a = 6, b = 11, c = 17
  8. What are the values of a[3], *p, *p + 1 and *(p+1) ?

    	  int a[10] = {2,4,6};
    	  int *p = a;
    	
    1. a[3] is 0, *p is 2, *p + 1 is 3, *(p+1) is 4
    2. a[3] is 6, *p is 2, *p + 1 is 3, *(p+1) is 4
    3. a[3] is 0, *p is 2, *p + 1 is 4, *(p+1) is 4
    4. a[3] is 6, *p is 2, *p + 1 is 4, *(p+1) is 4
    5. a[3] is 0, *p is 2, *p + 1 is 3, *(p+1) is 6
    6. a[3] is 6, *p is 2, *p + 1 is 4, *(p+1) is 6
  9. What is the output of the following code fragment?

    int b[] = {10,11,12,13,14,15};
    int *p;
    p = &a[2];
    for(int k = 0; k < 3; k++) {
      cout << p[k] << " ";
    }
    
    1. 10 11 12
    2. 11 12 13
    3. 12 13 14
    4. 13 14 15
  10. What is strlen(a)?

    	  char a[5] = {'A', 'B', '\0', 'C', 'D', '\0'};
    	
    1. 6
    2. 5
    3. 4
    4. 3
    5. 2
    6. Run time error
  11. What is int n = strcmp(s, t)?

    	  char s[] = "Hello";
    	  char t[] = "Help";
    	
    1. n > 0
    2. n == 0
    3. n < 0
  12. What is the value of s < t?

    #include <string>
    using namespace std;
    
    int main()
    {
      string s = "Hello";
      string t = "Help";
      ...
    }
    	
    1. true
    2. false
    3. Compiler Error
  13. A class Pair has a public member, int getX(). If p is a pointer to a Pair instance, which of the following are correct way(s) to call this member function using the pointer p and assign the return value to n?

    	  Pair m;
    	  int n;
    	  Pair *p;
    
    	  p = &m;
    	  n = *p.getX();    // (ans1)
    	  n = (*p).getX();  // (ans2)
    	  n = p.getX();     // (ans3)
    	  n = p->getX(); // (ans4)
    
    	
    1. only ans1 and ans4
    2. only ans2 and ans4
    3. only ans1
    4. only ans2
    5. only ans3
    6. only ans4
  14. A class Date is defined below. Which line has a syntax error?

    	  1	class Date
    	  2	{
    	  3	public:
    	  4	  int y;
    	  5	  int m;
    	  6	  int d;
    	  7	  Date();
    	  8	  Date(int yr, int mth, int day);
    	  9	}
    	
    1. line 1
    2. line 3
    3. line 7
    4. line 8
    5. line 9
  15. Which data members of the class A below are private?

    	  1	class A
    	  2	{
    	  3	  int w;
    	  4	public:
    	  5	  int x;
    	  6	private:
    	  7	  int y;
    	  8	  int z;
    	  9	};
    	
    1. only y
    2. only y and z
    3. only w and y
    4. all except x
  16. For the class B defined below, which lines in main will cause a compiler error?

    	  1	class B
    	  2	{
    	  3	private:
    	  4	  int x;
    	  5	public:
    	  6	  int y;
    	  7	  int z;
    	  8	};
    	  9	
    	  10	int main()
    	  11	{
    	  12	  B b;
    	  13	  b.x = 5;
    	  14	  x = 5;
    	  15	  b.y = 10;
    	  16	  b.z = 15;
    	  17	
    	  18	}
    	
    1. line 13 only
    2. line 14 only
    3. line 13 and line 14 only
    4. all lines 13 - 16
  17. Which one of the following is false?

    1. The purpose of a class constructor is to initialize the data members of the class.
    2. The return type of a constructor must be void.
    3. The name of a class constructor must be the same as the class name.
    4. A class can have more than one constructor.
  18. Here is a class Quad. It has two private members of type Pair. The only public constructor in Pair takes two integer parameters. There is no no-argument constructor. Here are two candidate ways of writing a constructor for Quad. Are both candidates correct?

    class Quad
    {
      Pair pr1;
      Pair pr2;
    public:
      Quad(int x1, int y1, int x2, int y2);
      ...
    };
    // Candidate A
    Quad::Quad(int x1, int y1, int x2, int y2) 
      : pr1(x1, y1), pr2(x2, y2)
    { }
    
    // Candidate B
    Quad::Quad(int x1, int y1, int x2, int y2) 
    { 
      pr1 = Pair(x1, y1);
      pr2 = Pair(x2, y2);
    }
    
    
    1. Either A or B would be correct.
    2. Only A would be correct.
    3. Only B would be correct.
    4. Neither A or B would be correct.
  19. Suppose the Quad class had a static Pair member named zero which should be initialized with both integers being 0.

    Which of the following would be a correct way of initializing this static member?

    1. In a no-argument Quad() constructor, zero = Pair(0,0);
    2. Include zero(0,0) in the initialization list of each constructor. zero(0,0)
    3. Inside the class defintion (Quad.h) include an initialization with the declaration of zero:
      class Quad
      {
        static Pair zero(0,0);
        ...
      };	      
      	    
    4. Outside the Quad definition (in Quad.cpp) write,
      Pair Quad::zero(0,0);	      
      	    
    5. Outside the Quad definition (in Quad.cpp) write,
      static Pair Quad::zero(0,0);	      
      	    
  20. For the class Employee definition and main routine below, what is the output?

    	  1	class Employee
    	  2	{
    	  3	  char nm[20];
    	  4	  int id;
    	  5	public:
    	  6	  Employee()
    	  7	    {
    	  8	      strcpy(nm, "missing");
    	  9	      id = 999999;
    	  10	
    	  11	    }
    	  12	  Employee(char name[], int empId)
    	  13	    {
    	  14	      strcpy(nm, name);
    	  15	      id = empId;
    	  16	    }
    	  17	  int getId() { return id; }
    	  18	  const char * getName() const { return nm; }
    	  19	};
    	  20	
    	  21	int main()
    	  22	{
    	  23	  Employee e1, e2;
    	  24	  Employee e3("Al", 555111);
    	  25	
    	  26	  e2 = Employee("Joan", 561222);
    	  27	  cout << e1.getId() << " "
    	  28	       << e2.getId() << " "
    	  29	       << e3.getId() << " " << endl;
    	  30	
    	  31	  return 0;
    	  32	
    	  33	}
    	
    1. 999999 999999 555111
    2. 999999 561222 561222
    3. 561222 555111
    4. 999999 561222 555111
  21. The class definition of Employee shown above is revised to show only the member function declarations (not their definitions).

    	  
    	  1   class Employee
    	  2   {
    	  3     char nm[20];
    	  4     int id;
    	  5   public:
    	  6     Employee();
    	  7     Employee(char name[], int empId);
    	  8     int getId() const;
    	  9     const char * getName() const;
    	  10   };
    	

    File Employee.cpp should contain the definitions of the member functions. In that file, the definition of getId() should be:

    1. int getId() const { return id; }
    2. int Employee.getId() const { return id; }
    3. Employee::int getId() const { return id; }
    4. int Employee::getId() const { return id; }
  22. If A is a class and an array is declared by:

    	  A arr[10];
    	

    which one of the following is false?

    1. class A must have a have a default constructor.
    2. The following code will swap the elements in arr[i] and arr[j].
      	      int tmp;
      	      tmp = arr[i];
      	      arr[i] = arr[j];
      	      arr[j] = tmp;
      	    
    3. A function f returns an int. The function f also has two parameters: an array of elements of type A and the number of elements in the array. The function f could be correctly declared like this:
      	      int f(A a[], int n);
      	    
    4. If A is a class and a and b are variables of type A, then the assignment: a = b; will copy each data member of b into the corresponding data member of a.
  23. A Date is given as

    	  class Date
    	  {
    	  int year;
    	  int month;
    	  int day;
    	  public:
    	  Date();
    	  Date(int m, int d, int y);
    	  int getYear() const;
    	  int getMonth() const;
    	  int getDay() const;
    	  };
    	

    If d1 is declared to be of type Date in main:

    	  int main()
    	  {
    	  Date d1;
    	  ...
    	  }
    	

    which one of the following is a legal assignment?

    1. d1.year = 2006;
    2. d1 = Date(2006);
    3. d1 = (2,9,2006);
    4. d1 = Date(2,9,2006);
  24. Referring to the Date class above, which of the following lines in the main function is an incorrect way to call member function getYear()of the Date class?

    	  1	int main()
    	  2	{
    	  3	  Date d1, d2;
    	  4	  Date d3(2,5,2006);
    	  5	
    	  6	  cout << Date.getYear() << endl;
    	  7	  cout << d1.getYear() << endl;
    	  8	  cout << d2.getYear() << endl;
    	  9	  cout << d3.getYear() << endl;
    	  10	
    	  11	}
    	
    1. Line 6
    2. Line 7
    3. Line 8
    4. Line 9
  25. Which of the following best describes the role of class constructors?

    1. The purpose of a class constructor is to allocate memory for values of that class type.
    2. The purpose of a class constructor is to initialize exactly one data member of the class.
    3. The purpose of a constructor for a class is to initialize the data members of a class value just after memory is allocated for the value.
    4. The purpose of a constructor for a class is to allocate memory for the data members of a class value and to initialize them.
  26. (Default parameter values)

    The function g below is called three times as shown. What are the three return values?

    	  1	int g(int x, int y = 5, int z = 10);
    	  2	
    	  3	int g(int x, int y, int z)
    	  4	{
    	  5	  return x * y * z;
    	  6	}
    	  7	
    	  8	int main()
    	  9	{
    	  10	  cout << g(1) << " "
    	  11	       << g(1,2) << " "
    	  12	       << g(1,2,3) << " ";
    	  13	
    	  14	  return 0;
    	  15	}
    	
    1. No output, the calls at lines 10 and 11 are illegal
    2. 1 2 6
    3. 50 50 50
    4. 1 20 50
    5. 50 20 6
    6. 50 20 1
  27. Assertion: If a parameter of a function has a default value, all following parameters must also provide a default value.

    1. True
    2. False
  28. A class M has only one constructor:

    	  class M
    	  {
    	  ...
    	  public:
    	  M(int x);
    	  ...
    	  };
    	

    Which of the following declarations in main is legal?

    	  M a;
    	  M b[5];
    	  M c(5);
    	
    1. Only declarations of a and c
    2. Only declarations of b and c
    3. Only the delcaration of c
    4. All three delcarations are legal
  29. Suppose the single constructor in class M is modified like this:
    	  class M
    	  {
    	  ...
    	  public:
    	  M(int x = 1);
    	  ...
    	  };
    
    	

    Now which of the following declarations in main is legal?

    	  M a;
    	  M b[5];
    	  M c(5);
    	
    1. Only declarations of a and c
    2. Only declarations of b and c
    3. Only the delcaration of c
    4. All three delcarations are legal
  30. The following declaration of an Emp class is in file Emp.h, its implementation is in the file Emp.cpp.

    	  1	class Emp
    	  2	{
    	  3	  static int nextid;
    	  4	  static int numEmps;
    	  5	  int id;
    	  6	public:
    	  7	  Emp();
    	  8	  int getId() const;
    	  9	  static int numCreated();
    	  10	};
    	

    The nextid variable should be initialized exactly once to the value 1001 for the whole program no matter how many Emp values are created. Each time an Emp value is created the value of nextid should be used to initialize that Emp value's id. Then nextid should be incremented.

    The numEmps variable should be initialized to 0 exactly once for the whole program and should be incremented each time an Emp value is created.

    Which one of the following is a correct statement?

    1. All three variables nextid, numEmps and id should be initialized in the constructor Emp in the Emp.cpp file like this:
      	      Emp::Emp()
      	      {
      	      numEmps = 0;
      	      nextid = 1001;
      	      id = nextid;
      	      numEmps++;
      	      nextid++;
      	      }
      	    
    2. The id variable should be initialized in the constructor Emp in the Emp.cpp file; nextid and numEmps should be initialized in the Emp.h file but after the Emp class like this:
      	      
      	      Emp::Emp()
      	      {
      	      id = nextid++;
      	      numEmps++;
      	      }
      
      	      
      	      class Emp 
      	      {
      	      ...
      	      };
      	      static int Emp::nextid = 1001;
      	      static int Emp::numEmps = 0;
      	    
    3. The id variable should be initialized in the constructor Emp in the Emp.cpp file; nextid and numEmps should be initialized in the Emp.cpp file outside any function like this:
       
      
      	      Emp::Emp()
      	      {
      	      id = nextid++;
      	      numEmps++;
      	      }
      
      	      static int nextid = 1001; 
      	      static int numEmps = 0;
      
      	    
    4. The id variable should be initialized in the constructor Emp in the Emp.cpp file; nextid and numEmps should be initialized in the Emp.cpp file outside any function like this:
      
      	      Emp::Emp()
      	      {
      	      id = nextid++;
      	      numEmps++;
      	      }
      
      	      static int Emp::nextid = 1001; 
      	      static int Emp::numEmps = 0;
      	    
    5. The id variable should be initialized in the constructor Emp in the Emp.cpp file; nextid and numEmps should be initialized in the Emp.cpp file outside any function like this:
      
      	      Emp::Emp()
      	      {
      	      id = nextid++;
      	      numEmps++;
      	      }
      
      	      int Emp::nextid = 1001; 
      	      int Emp::numEmps = 0;
      	    
  31. Consider the statements:

    	  (i) The getId() member function of Emp can access all three data
    	  members: id and the static data members nextid and numEmps.
    	  (ii) The getId() member function of Emp can access only the
    	  non-static data member id.
    	  (iii) The numCreated static member function of Emp can access all
    	  three data members: id and the static members nextid and numEmps.
    	  (iv) The numCreated static member function of Emp can access only the
    	  static data members: nextid and numEmps.
    	
    1. (i) and (iii) are true
    2. (i) and (iv) are true
    3. (ii) and (iii) are true
    4. (ii) and (iv) are true
  32. Calling the static numCreated member function. Which one of the assertions below is true:

    	  1	   int main()
    	  2	   {
    	  3	     Emp e1, e2;
    	  4	
    	  5	     cout << Emp::numCreated() << endl;
    	  6	     cout << e1.numCreated() << endl;
    	  7	     ...
    	  8	   }
    
    	
    1. Function call at line 5 is legal, but line 6 is illegal
    2. Function call at line 6 is legal, but line 5 is illegal
    3. Function call at line 5 and at line 6 are both legal.
  33. For the Emp class above, assuming the static variables are initialized and maintained as in problem xx, what is the output of the following program?

    	  int main()
    	  {
    
    	  Emp e1, e2, e3;
    
    	  cout << "Employee: " << e1.getId() << "; "
    	  << "Employee: " << e3.getId() << endl;
    
    	  return 0;
    
    	  }
    	
    1. Employee: 1001; Employee: 1001
    2. Employee: 1001; Employee: 1002
    3. Employee: 1001; Employee: 1003
    4. Employee: 1002; Employee: 1002
    5. Employee: 1003; Employee: 1003
  34. Consider the assetion:

    	  Here are 3 candidates for how to write the static member function
    	  numCreated in the Emp.cpp file:
    
    	  
    
    	  int Emp::numCreated()
    	  {
    	  return this->numEmps;
    	  }
    
    	  
    	  int Emp::numCreated()
    	  {
    	  return numEmps;
    	  }
    
    	  
    	  static int Emp::numCreated()
    	  {
    	  return numEmps;
    	  }
    
    
    	
    1. Only Version 1 is legal
    2. Only Version 2 is legal
    3. Only Versions 1 and 2 are legal
    4. Only Version 3 is legal
  35. This question and following ones below refer to the class mstring which is intended to define a string type that grows dynamically. It does this by having a private data member that is a dynamic array of char that is null terminated. The len data member keeps track of the number of characters stored not including the null byte and capacity + 1 is the actual size of the array.

    	  1	class mstring
    	  2	{
    	  3	  char *str;
    	  4	  int len;
    	  5	public:
    	  6	  mstring();
    	  7	  mstring(const char *s);
    	  8	  mstring(const mstring* other);
    	  9	  ...
    	  10	  ~mstring();
    	  11	};
    	

    Which is the copy constructor?

    1. line 6
    2. line 7
    3. line 8
    4. line 7 and line 8
    5. There is no copy constructor.

  36. True or false. The constructor at line 8 could also have been written to have its argument passed by value:
    	      mstring(mstring other);
    	    
    1. True
    2. False
  37. A copy constructor is used at which lines of the following code:

    	      1	void f(const mstring& w);
    	      2	void g(mstring s);
    	      3	int main()
    	      4	{
    	      5	  mstring a("Hello");
    	      6	  mstring b(a);
    	      7	  mstring c;
    	      8	
    	      9	  c = a;
    	      10	
    	      11	  f(a);
    	      12	  g(a);
    	      13	
    	      14	  return 0;
    	      15	}
    	    
    1. lines 5, 6, and 9
    2. lines 6 and 9
    3. lines 6, 9, 11 and 12
    4. lines 6 and 11
    5. lines 6 and 12
    6. lines 9 and 11
    7. lines 9 and 12
    next prev start
  38. When does the destructor for mstring execute for the code below?

    	      1	void f(const mstring& w)
    	      2	{
    	      3	  cout << w << endl;
    	      4	}
    	      5	
    	      6	void g(mstring s)
    	      7	{
    	      8	  cout << s << endl;
    	      9	}
    	      10	
    	      11	void h()
    	      12	{
    	      13	  mstring a("Hello");
    	      14	  mstring b("World");
    	      15	  f(a);
    	      16	  g(b);
    	      17	
    	      18	}
    	      19	int main()
    	      20	{
    	      21	  h();
    	      22	  return 0;
    	      23	}
    	    
    1. once when f returns; once when g returns; and once when h returns.
    2. once when f returns; once when g returns; and twice when h returns.
    3. once when f returns; and once when h returns.
    4. once when g returns; and once when h returns.
    5. once when f returns; and twice when h returns.
    6. once when g returns; and twice when h returns.
  39.  

    When does the destructor for mstring execute in the code below:

    	      1	mstring* h()
    	      2	{
    	      3	  mstring *pa;
    	      4	  pa = new mstring("Hello");
    	      5	  return pa;
    	      6	
    	      7	}
    	      8	int main()
    	      9	{
    	      10	  mstring *ps;
    	      11	
    	      12	  ps = h();
    	      13	  cout << *ps << endl;
    	      14	  delete ps;
    	      15	  return 0;
    	      16	}
    	    
    1. When h returns and at line 12
    2. When h returns and at line 14
    3. At line 12
    4. At line 14
    5. When main returns
  40. For the Emp class below, which one of the following is not syntactically correct implementation of the constructor?

    	      1	class Emp
    	      2	{
    	      3	  string fname;
    	      4	  string lname;
    	      5	  double salary;
    	      6	public:
    	      7	  Emp(const string& fn, const string& ln, double sal);
    	      8	  string getFname();
    	      9	  string getLname();
    	      10	  double getSalary();
    	      11	  void setSalary(double sal);
    	      12	  void incrSalary(double sal);
    	      13	};
    	    
    1. 		  Emp::Emp(const string& fn, const string& ln, double sal)
      		  : fname(fn), lname(ln), salary(sal) {}
      		
    2. 		  Emp::Emp(const string& fn, const string& ln, double sal)
      		  : fn(fname), ln(lname), sal(salary) {}
      		
    3. 		  Emp::Emp(const string& fn, const string& ln, double sal)
      		  : salary(sal)
      		  {
      		  fname = fn;
      		  lname = ln;
      		  }
      		
    4. 		  Emp::Emp(const string& fn, const string& ln, double sal)
      		  {
      		  fname = fn;
      		  lname = ln;
      		  salary = sal;
      		  }
      		
Printing the rest...1264 lines out of 1266