C Variable Declaration and Scope

Last Updated :
Discuss
Comments

Question 1

Predict the output C
#include <stdio.h>
int var = 20;
int main()
{
    int var = var;
    printf("%d ", var);
    return 0;
}
  • Garbage Value
  • 20
  • Compiler Error

Question 2

Consider the program below in a hypothetical language which allows global variable and a choice of call by reference or call by value methods of parameter passing.

C
 int i ;
program main ()
{
    int j = 60;
    i = 50;
    call f (i, j);
    print i, j;
}
procedure f (x, y)
{           
    i = 100;
    x = 10;
    y = y + i ;
}

Which one of the following options represents the correct output of the program for the two parameter passing mechanisms?

  • Call by value : i = 70, j = 10; Call by reference : i = 60, j = 70

  • Call by value : i = 50, j = 60; Call by reference : i = 50, j = 70

  • Call by value : i = 10, j = 70; Call by reference : i = 100, j = 60

  • Call by value : i = 100, j = 60; Call by reference : i = 10, j = 70

Question 3

Consider the following statements S1, S2 and S3 : S1 : In call-by-value, anything that is passed into a function call is unchanged in the caller’s scope when the function returns. S2 : In call-by-reference, a function receives implicit reference to a variable used as argument. S3 : In call-by-reference, caller is unable to see the modified variable used as argument.
  • S3 and S2 are true.
  • S3 and S1 are true.
  • S2 and S1 are true.
  • S1, S2, S3 are true.

Question 4

When an array is passed as parameter to a function, which of the following statements is correct?
  • The function can change values in the original array.
  • In C, parameters are passed by value, the function cannot change the original value in the array.
  • It results in compilation error when the function tries to access the elements in the array.
  • Results in a run time error when the function tries to access the elements in the array.

Question 5

Assume that the program ‘P’ is implementing parameter passing with ‘call by reference’. What will be printed by following print statements in P? Program P( ) { x = 10; y = 3; funb (y, x, x) print x; print y; } funb (x, y, z) { y = y + 4; z = x + y + z; }
  • 10, 7
  • 31, 3
  • 10, 3
  • 31, 7

Question 6

Given i= 0, j = 1, k = – 1 x = 0.5, y = 0.0 What is the output of given ‘C’ expression ? x * 3 & & 3 || j | k
  • -1
  • 0
  • 1
  • 2

Question 7

Find out the correct statement for the following program. C
#include "stdio.h"

int * gPtr;

int main()
{
 int * lPtr = NULL;

 if(gPtr == lPtr)
 {
   printf("Equal!");
 }
 else
 {
  printf("Not Equal");
 }

 return 0;
}
  • It’ll always print Equal.
  • It’ll always print Not Equal.
  • Since gPtr isn’t initialized in the program, it’ll print sometimes Equal and at other times Not Equal.

Question 8

Consider the following variable declarations and definitions in C

C
i) int var_9 = 1;
ii) int 9_var = 2;
iii) int _ = 3;

Choose the correct statement w.r.t. above variables.

  • Both i) and iii) are valid.

  • Only i) is valid.

  • Both i) and ii) are valid.

  • All are valid.

Question 9

Consider the following C program, which variable has the longest scope? C
int a;
int main()
{
   int b;
   // ..
   // ..
}
int c;
  • a
  • b
  • c
  • All have same scope

Question 10

C
int main()
{
  int x = 032;
  printf("%d", x);
  return 0;
}
  • 32
  • 0
  • 26
  • 50
Tags:

There are 15 questions to complete.

Take a part in the ongoing discussion