Variables, Data Types, Constants and Operators


  1. What is wrong with this program?

    
    main()
    {
    	p=47;
    	cout << p;
    }
    

  2. What is wrong with this declaration?

    
    int x = y = 95;
    

  3. Write four different C++ statements that subtract 1 from the integer variable x.

  4. Which of the following are valid C++ statements? Evaluate the ones which are valid.
    • x = 65/5*2
    • 65 / 4 = k;
    • p = 65(4*7);
    • x = 34 - 4 * 7;
    • x = 34 - ( 4 * 7);
    • 54 % 5 = h ;
    • x = 54 % 7;


Answers

	




































    1. The #include <iostream.h> is missing and the variable p has not been declared.

    2. The equals sign can only be used in a declaration to initialise a variable. Therefore it should be
      
      int	x=95, y =95;
      

    3. x = x - 1;
      x --;
      --x;
      x -= 1;
      

      • missing ;
      • not valid - should be k = 64/4;
      • not valid
      • valid x = 6
      • valid
      • not valid
      • valid x = 5


    Back to Questions Back to Main Page for Lecture 2