Variables and Data Types

1. Which of the following are valid variable declarations?

1. double celsius = 37.2;

Ans: Valid.

2. int rank = 1

Ans: Invalid, missing terminating semicolon

3. kelvin = 273;

Ans: Invalid, missing data type

4. int count = 7;

Ans: Valid

5. double int pi = 3.14;

Ans: Invalid, data type can be double or int, not both

6. int double = 9;

Ans: Invalid, double is a keyword and hence cannot be used as a variable name

 

2. Write a program that declares an int variable named runs and initialises it to zero. Print its value. Next, assign value 50 to variable runs and print its value again.

#include<stdio.h>

int main (int argc, char *argv[]){
    int runs = 0;
    printf("Value of runs after initialization %d\n", runs);
    runs = 50;
    printf("Value of runs after assignment %d\n", runs);
    return 0;
}

 

3. Which of the following are valid variable declarations?

1. double average, int maths, science;

Ans: Invalid, a declaration can have only one data type that is specified only once at the beginning of the statement.

2. int maths, int science;

Ans: Invalid, a declaration can have only one data type that is specified only once at the beginning of the statement.

3. int average, temperature = 32;

Ans: Valid. average is uninitialized while temperature has been initialized.

4. int maths, science, english;

Ans: Valid. This statement declares three int variables that have been left uninitialized.

 

4. What will be the output of following program?

#include <stdio.h>

int main (int argc, char * argv[]) {
    int physics = 9, maths = 8;
    double average = 8.5;
    printf("physics is %d, maths is %d, and average is %f\n", physics, maths, average);
    return 0;
}

Ans: The printf statement simply prints value of 3 variables embedded in the message. From positional matching, we know that first %d in format string corresponds to int variable physics, second %d in format string corresponds to int variable maths and finally %f corresponds to double variable average. Hence, following message is printed.

physics is 9, maths is 8, and average is 8.500000

 

5.What will be the output of following program?

#include <stdio.h>

int main (int argc, char * argv[]) {
    int test = 9;
    printf("test = %d\n", test);
    test = 10;
    printf("test = %d\n", test);
    return 0;
}

Ans: When first printf statement is encountered, value in variable test is 9. Hence first printf statement prints test = 9. After first printf statement is executed, we update value in variable test to 10. Hence second printf statement prints test = 10. Below is the output printed by the program.

test = 9

test = 10

 

6. Which among the following are valid variable names?

1. totalscore1

Ans: Valid.

2. Total_score_1

Ans: Valid.

3. _total_score__1

Ans: Valid.

4. total score

Ans: Invalid. Variable name cannot contain spaces.

5. 1_total_score

Ans: Invalid. Variable name cannot begin with a numeric character.

6. _

Ans: Valid.

7. 115

Ans: Invalid. Variable name cannot begin with a numeric character.

8. _115

Ans: Valid

9. int

Ans: Invalid. int is a keyword.

10. Int

Ans: Valid. C is case sensitive, int is a keyword but Int is not.

11. __

Ans: Valid.

 

7. Trace retrieval of values for variables maths and total_marks in the printf statement in above example.

Ans:

maths

To retrieve value of variable maths, system consults the symbol table. It finds that maths is stored at address 40. System retrieves values at address 40 – which is 8.

total_marks

To retrieve value of variable total_marks, system consults the symbol table. It finds that total_marks is stored at address 16. System retrieves values at address 16 – which is 17.

 

8. Which of the following statements are true.

1. Initializing a variable is optional.

Ans: True.

2. All uninitialized variables are by default initialized to 0 by the system.

Ans: False. Any uninitialized variables will contain whatever value was present at that memory location at the time of allocation.

3. It is mandatory to declare a variable before using it.

Ans: True.

4. A variable can be declared anywhere in a program.

Ans: False. Variable can be declared only at the beginning of a code block.

5. As a good programming practice, we should always initialize all our variables.

Ans: True