12

Input and Output

Question 1
Predict the output of following program?
#include "stdio.h"
int main()
{
    char arr[100];
    printf("%d", scanf("%s", arr));
    /* Suppose that input value given
        for above scanf is "GeeksQuiz" */
    return 1;
}
A
9
B
1
C
10
D
100
Input and Output    
Discuss it


Question 1 Explanation: 
In C, scanf returns the no. of inputs it has successfully read. See http://geeksforgeeks.org/archives/674
Question 2
Predict output of the following program
#include <stdio.h>
int main()
{
   printf("\new_c_question\by");
   printf("\rgeeksforgeeks"); 
   getchar();
   return 0;
}
A
ew_c_question
geeksforgeeks
B
new_c_ques
geeksforgeeks
C

geeksforgeeks
D
Depends on terminal configuration
Input and Output    
Discuss it


Question 3
#include <stdio.h>

int main() 
{ 
  printf(" \"GEEKS %% FOR %% GEEKS\""); 
  getchar(); 
  return 0; 
}
A
“GEEKS % FOR % GEEKS”
B
GEEKS % FOR % GEEKS
C
\"GEEKS %% FOR %% GEEKS\"
D
GEEKS %% FOR %% GEEKS
Input and Output    
Discuss it


Question 3 Explanation: 
Backslash (\\\\) works as escape character for double quote (“). For explanation of %%, see http://www.geeksforgeeks.org/how-to-print-using-printf/
Question 4
#include <stdio.h>
// Assume base address of "GeeksQuiz" to be 1000
int main()
{
   printf(5 + "GeeksQuiz");
   return 0;
}
A
GeeksQuiz
B
Quiz
C
1005
D
Compile-time error
Input and Output    
Discuss it


Question 4 Explanation: 

printf is a library function defined under stdio.h header file. The compiler adds 5 to the base address of the string through the expression 5 + "GeeksQuiz" . Then the string "Quiz" gets passed to the standard library function as an argument.

Question 5
Predict the output of the below program:
#include <stdio.h>

int main()
{
    printf("%c ", 5["GeeksQuiz"]);
    return 0;
}
A
Compile-time error
B
Runtime error
C
Q
D
s
Input and Output    
Discuss it


Question 5 Explanation: 
The crux of the program lies in the expression: 5["GeeksQuiz"] This expression is broken down by the compiler as: *(5 + "GeeksQuiz"). Adding 5 to the base address of the string increments the pointer(lets say a pointer was pointing to the start(G) of the string initially) to point to Q. Applying value-of operator gives the character at the location pointed to by the pointer i.e. Q.
Question 6
Predict the output of below program:
#include <stdio.h>
int main()
{
    printf("%c ", "GeeksQuiz"[5]);
    return 0;
}
A
Compile-time error
B
Runtime error
C
Q
D
s
Input and Output    
Discuss it


Question 6 Explanation: 
The crux of the program lies in the expression: "GeeksQuiz"[5]. This expression is broken down by the compiler as: *(“GeeksQuiz” + 5). Adding 5 to the base address of the string increments the pointer(lets say a pointer was pointing to the start(G) of the string initially) to point to Q. Applying value-of operator gives the character at the location pointed to by the pointer i.e. Q.
Question 7
Which of the following is true
A
gets() can read a string with newline chacters but a normal scanf() with %s can not.
B
gets() can read a string with spaces but a normal scanf() with %s can not.
C
gets() can always replace scanf() without any additional code.
D
None of the above
Input and Output    
Discuss it


Question 7 Explanation: 
gets() can read a string with spaces but a normal scanf() with %s can not. Consider following program as an example. If we enter "Geeks Quiz" as input in below program, the program prints "Geeks" 1 But in the following program, if we enter "Geeks Quiz", it prints "Geeks Quiz" 1
Question 8
Which of the following is true
A
gets() doesn't do any array bound testing and should not be used.
B
fgets() should be used in place of gets() only for files, otherwise gets() is fine
C
gets() cannot read strings with spaces
D
None of the above
Input and Output    
Discuss it


Question 8 Explanation: 
Question 9
What does the following C statement mean?
 scanf("%4s", str);
A
Read exactly 4 characters from console.
B
Read maximum 4 characters from console.
C
Read a string str in multiples of 4
D
Nothing
Input and Output    
Discuss it


Question 9 Explanation: 
Try following program, enter GeeksQuiz, the output would be "Geek"
#include <stdio.h>

int main()
{
    char str[50] = {0};
    scanf("%4s", str);
    printf(str);
    getchar();
    return 0;
}

Question 10
#include<stdio.h>

int main()
{
    char *s = "Geeks Quiz";
    int n = 7;
    printf("%.*s", n, s);
    return 0;
}
A
Geeks Quiz
B
Nothing is printed
C
Geeks Q
D
Geeks Qu
Input and Output    
Discuss it


Question 10 Explanation: 
.* means The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
There are 15 questions to complete.
12

Company Wise Coding Practice    Topic Wise Coding Practice