• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

C Input and Output

Question 11

Consider the following code. The function myStrcat concatenates two strings. It appends all characters of b to end of a. So the expected output is "Geeks Quiz". The program compiles fine but produces segmentation fault when run.
C
void myStrcat(char *a, char *b)
{
    int m = strlen(a);
    int n = strlen(b);
    int i;
    for (i = 0; i <= n; i++)
       a[m+i]  = b[i];
}

int main()
{
    char *str1 = \"Geeks \";
    char *str2 = \"Quiz\";
    myStrcat(str1, str2);
    printf(\"%s \", str1);
    return 0;
}
Which of the following changes can correct the program so that it prints "Geeks Quiz"?
  • char *str1 = "Geeks "; can be changed to char str1[100] = "Geeks ";
  • char *str1 = "Geeks "; can be changed to char str1[100] = "Geeks "; and a line a[m+n-1] = \'\\0\' is added at the end of myStrcat
  • A line a[m+n-1] = \'\\0\' is added at the end of myStrcat
  • A line \'a = (char *)malloc(sizeof(char)*(strlen(a) + strlen(b) + 1)) is added at the beginning of myStrcat()

Question 12

What is output of following C - Code?
C
#include <stdio.h>
#include <stdarg.h>
int fun(int n, ...)
{
    int i, j = 1, val = 0;
    va_list p;
    va_start(p, n);
    for (; j < n; ++j)
    {
        i = va_arg(p, int);
        val += i;
    }
    va_end(p);
    return val;
}
int main()
{
    printf(\"%d\\n\", fun(4, 1, 2, 3));
    return 0;
}
  • 3
  • 5
  • 6
  • 10

Question 13

What’s going to happen when we compile and run the following C program snippet?
C
#include \"stdio.h\"
int main()
{
 int a = 10;
 int b = 15;

 printf(\"=%d\",(a+1),(b=a+2));
 printf(\" %d=\",b);

 return 0;
}
  • =11 15=
  • =11 12=
  • Compiler Error due to (b=a+2) in the first printf().
  • No compile error but output would be =11 X= where X would depend on compiler implementation.

Question 14

Consider the following C code. Assume that unsigned long int type length is 64 bits.
C
unsigned long int fun(unsigned long int n) {
        unsigned long int i, j = 0, sum = 0;
        for( i = n; i > 1; i = i/2) j++;
        for( ; j > 1; j = j/2) sum++;
        return sum;
}
The value returned when we call fun with the input 240 is
  • 4
  • 5
  • 6
  • 40

Question 15

Consider the following C program: C
#include <stdio.h>
void fun1(char *s1, char *s2) {
  char *temp;
  temp = s1;
  s1 = s2;
  s2 = temp;
}
void fun2(char **s1, char **s2) {
  char *temp;
  temp = *s1;
  *s1 = *s2;
  *s2 = temp;
}
int main() {
  char *str1 = \"Hi\", *str2 = \"Bye\";
  fun1(str1, str2);
  printf(\"%s %s\", str1, str2);
  fun2(&str1, &str2);
  printf(\"%s %s\", str1, str2);
  return 0;
}
The output of the program above is
  • Hi Bye Bye Hi
  • Hi Bye Hi Bye
  • Bye Hi Hi Bye
  • Bye Hi Bye Hi

Question 16

What is the value returned by the function f given below when n = 100? int f (int n) { if (n = = 0) then return n; else return n + f(n-2); }
  • 2550
  • 2556
  • 5220
  • 5520

Question 17

What will be the output of the following ‘C’ code?

C
#include <stdio.h>

int main()
{
    int x = 128;
    printf("\n%d", 1 + x++);
    
    return 0;
}
  • 128

  • 129

  • 130

  • 131

Question 18

Why Java is Partially OOP language?
  • It allows code to be written outside classes

  • It supports usual declaration of primitive data types
     

  • It does not support pointers

  • It doesn’t support all types of inheritance

Question 19

Consider the following C functions.
C
int tob (int b, int* arr) {
    int i;
    for (i = 0; b>0; i++)  {
        if (b%2)  arr [i] = 1;
        else      arr[i] = 0;
        b = b/2;
    }
    return (i);
}
 

int pp(int a, int b)  {
    int  arr[20];
    int i, tot = 1, ex, len;
    ex = a;
    len = tob(b, arr);
    for (i=0; i<len ; i++) {
         if (arr[i] ==1)
             tot = tot * ex;
         ex= ex*ex;
    }
return (tot) ;
}
The value returned by pp(3,4) is ________ . Note - This question was Numerical Type.
  • 81
  • 64
  • 100
  • 49

Question 20

Consider the following C program.
 

C
#include <stdio.h>

int main()
{
    int i, j, count;
    count = 0;
    i = 0;
    
    for (j = -3; j <= 3; j++)
    {
        if ((j >= 0) && (i++))
        {
            count = count + j;
        }
    }
    
    count = count + i;
    printf("%d", count);
    
    return 0;
}

Which one of the following options is correct?

  • The program will not compile successfully

  • The program will compile successfully and output 10 when executed

  • The program will compile successfully and output 8 when executed

  • The program will compile successfully and output 13 when executed

There are 37 questions to complete.

Last Updated :
Take a part in the ongoing discussion