edited by
20,055 views
85 85 votes

Consider the following "C" program.

void f(int, short);
void main()
{
    int i = 100;
    short s = 12;
    short *p = &s;
    ____________;  // call to f()
}

 Which one of the following expressions , when placed in the blank above, will NOT result in a type checking error?

  1. $f(s, *s)$
  2. $i = f(i,s)$
  3. $f(i, *s)$
  4. $f(i, *p)$

4 Answers

Best answer
127 127 votes
  1. $f(s, *s) - 1^{\text{st}}$ argument is short whereas the function expects an int. But in C language short gets implicitly converted to int during a function call and so this won’t be a type error. But second argument is a pointer (can be $64$ bits on a $\textsf{x64}$ machine) where as the function expects a short (can be even $16$ bits). So this will generate a type error. So, WRONG.
  2. $i = f(i,s)$ - return type is not void. So, WRONG.
  3. $f(i, \ast s) - 1^{\text{st}}$ argument is int, second is again syntax error. So, WRONG
  4. $f(i, \ast p)$ - Both the arguments and return type match. $p$ is a pointer to short, so $\ast p$ is value of short. So, D is ANSWER.
edited by
35 35 votes
By option elimination method:
Options A & C are wrong because *s is an invalid argument as s is not a pointer

Option B is incorrect because the f function return type is void while in option it shows of type int implicitly.

Option D is correct because f(100,12) is a valid function call for f(int, short).
edited by
2 2 votes

Let's check each option.

A. f(s, *s)

  • s is a short, which can be promoted to int for the first parameter.
  • *s is invalid because s is not a pointer.
  • Type error.

B. i = f(i, s)

  • f(i,s) is valid as a function call, but f returns void.
  • Assigning a void value to i is illegal.
  • Type error.

C. f(i, *s)

  • i matches int.
  • *s is invalid because s is not a pointer.
  • Type error.

D. f(i, *p)

  • i is int → matches first parameter.
  • p is short *, so *p is a short → matches second parameter.
  • No type checking error.

Answer: D. f(i, *p)

Answer:
Position:
Show:

Related questions

94 94 votes
5 answers 5 answers
30.2k
30.2k views
Sandeep Singh asked Feb 12, 2016
30,233 views
Consider the following C program.# include <stdio.h void mystery (int *ptra, int *ptrb) { int *temp; temp = ptrb; ptrb =ptra; ptra = temp; } int main () { int a...
58 58 votes
5 answers 5 answers
19.8k
19.8k views
Sandeep Singh asked Feb 12, 2016
19,809 views
Let $X$ be a recursive language and $Y$ be a recursively enumerable but not recursive language. Let $W$ and $Z$ be two languages such that $\overline{Y}$ reduces to $W$,...
28 28 votes
5 answers 5 answers
11.6k
11.6k views
Sandeep Singh asked Feb 12, 2016
11,600 views
Consider that $B$ wants to send a message $m$ that is digitally signed to $A$. Let the pair of private and public keys for $A$ and $B$ be denoted by ${K_{x}}^-$ and ${K_{...
52 52 votes
4 answers 4 answers
22.9k
22.9k views
Sandeep Singh asked Feb 12, 2016
22,854 views
Consider a computer system with $40$-bit virtual addressing and page size of sixteen kilobytes. If the computer system has a one-level page table per process and each pag...