Sie sind auf Seite 1von 12

DIT, Pimpri Department of Electrical Engineering

Subject: (203148) Numerical Methods & Computer Programming


MCQ Unit I
int fun(); is overrided with extern int
Declarations C.
fun();

1. Which of the following statements should be D. None of these


used to obtain a remainder after dividing 3.14
by 2.1 ? 5. How would you round off a value from 1.66
A. rem = 3.14 % 2.1; to 2.0?
A. ceil(1.66)
B. rem = modf(3.14, 2.1);
B. floor(1.66)
C. rem = fmod(3.14, 2.1);
C. roundup(1.66)
Remainder cannot be obtain in floating
D.
point division. D. roundto(1.66)

2. What are the types of linkages? 6. By default a real number is treated as a


A. Internal and External A. float
B. External, Internal and None B. double
C. External and None C. long double
D. Internal D. far double

3. Which of the following special symbol 7. Which of the following is not user defined
allowed in a variable name? data type?
A. * (asterisk) 1 : struct book
{
B. | (pipeline)
char name[10];
C. - (hyphen) float price;
int pages;
D. _ (underscore) };
2 : long int l = 2.35;
4. Is there any difference between following
3 : enum day {Sun, Mon, Tue, Wed};
declarations?
1 : extern int fun(); A. 1

2 : int fun(); B. 2
A. Both are identical C. 3
No difference, except extern int D. Both 1 and 2
B.
fun(); is probably in another file

1
DIT, Pimpri Department of Electrical Engineering

8. Is the following statement a declaration or 11. When we mention the prototype of a function?
definition? A. Defining
extern int i;
A. Declaration B. Declaring

B. Definition C. Prototyping

C. Function D. Calling

D. Error
Functions
9. Identify which of the following are
declarations 1. The keyword used to transfer control from a
1 : extern int x; function back to the calling function is
2 : float square ( float x ) { ... } A. switch
3 : double pow(double, double); B. goto
A. 1 C. go back
B. 2 D. return
C. 1 and 3
2. What is the notation for following functions?
D. 3 1. int f(int a, float b)
{
10. In the following program where is the /* Some code */
variable a getting defined and where it is }
getting declared?
#include<stdio.h> 2. int f(a, b)
int main() int a; float b;
{ {
extern int a; /* Some code */
printf("%d\n", a); }
return 0; 1. KR Notation
} A.
2. ANSI Notation
int a=20;
1. Pre ANSI C Notation
extern int a is declaration, int a = 20 is B.
A. 2. KR Notation
the definition
1. ANSI Notation
int a = 20 is declaration, extern int a is C.
B. 2. KR Notation
the definition
1. ANSI Notation
C. int a = 20 is definition, a is not defined D.
2. Pre ANSI Notation
D. a is declared, a is not defined

2
DIT, Pimpri Department of Electrical Engineering

3. How many times the program will print {


"IndiaBIX" ? return ( f2(20) );
#include<stdio.h> }
int f2(int a)
int main() {
{ return (a*a);
printf("IndiaBIX"); }
main(); A. Yes
return 0;
} B. No
A. Infinite times

B. 32767 times
Expressions
C. 65535 times
1. Which of the following is the correct order of
D. Till stack overflows evaluation for the below expression?
z=x+y*z/4%2-1
4. Functions cannot return a floating point A. * / % + - =
number
B. = * / % + -
A. Yes
C. / * % - + =
B. No
D. * % / - + =
5. Every function must return a value
A. Yes 2. Which of the following correctly shows the
hierarchy of arithmetic operations in C?
B. No
A. / + * -

6. If a function contains two return statements B. * - / +


successively, the compiler will generate
C. + - / *
warnings. Yes/No ?
A. Yes D. / * + -

B. No
3. Which of the following is the correct usage of
conditional operators used in C?
7. Maximum number of arguments that a
A. a>b ? c=30 : c=40;
function can take is 12
A. Yes B. a>b ? c=30;

B. No C. max = a>b ? a>c?a:c:b>c?b:c

D. return (a>b)?(a:b)
8. Will the following functions work?
int f1(int a, int b)
3
DIT, Pimpri Department of Electrical Engineering

4. Which of the following is the correct order if 2. (a <=20) ? b : (c = 30);


calling functions in the below code?
a = f1(23, 14) * f2(12/4) + f3(); A. Yes

A. f1, f2, f3 B. No

B. f3, f2, f1 Two different operators would always have


different Associativity.
Order may vary from compiler to
C. A. Yes
compiler
B. No
D. None of above

9. Will the expression *p = p be disallowed by


5. Which of the following are unary operators in
the compiler?
C?
A. Yes
1. !
2. sizeof B. No

3. ~
10. Every operator has an Associativity
4. &&
A. Yes
A. 1, 2
B. No
B. 1, 3

C. 2, 4 Input/output
D. 1, 2, 3 1. In a file contains the line "I am a boy\r\n" then
on reading this line into the
array str using fgets(). What will str contain?
6. In which order do the following gets evaluated A. "I am a boy\r\n\0"
1. Relational
B. "I am a boy\r\0"
2. Arithmetic
3. Logical C. "I am a boy\n\0"

4. Assignment D. "I am a boy"

A. 2134
2. What is the purpose of "rb" in fopen() function
B. 1234 used below in the code?
FILE *fp;
C. 4321 fp = fopen("source.txt", "rb");
D. 3214 open "source.txt" in binary mode for
A.
reading
7. Are the following two statement same? open "source.txt" in binary mode for
1. a <= 20 ? (b = 30): (c = 30); B.
reading and writing

4
DIT, Pimpri Department of Electrical Engineering

Create a new file "source.txt" for B. printf("%Lf %f", a, b);


C.
reading and writing
C. printf("%Lf %Lf", a, b);
D. None of above
D. printf("%f %Lf", a, b);

3. What does fp point to in the program ?


#include<stdio.h> 6. Which files will get closed through
the fclose() in the following program?
int main() #include<stdio.h>
{
FILE *fp; int main()
fp=fopen("trial", "r"); {
return 0; FILE *fs, *ft, *fp;
} fp = fopen("A.C", "r");
fs = fopen("B.C", "r");
A. The first character in the file ft = fopen("C.C", "r");
A structure which contains fclose(fp, fs, ft);
B. a char pointer which points to the first return 0;
character of a file. }
A. "A.C" "B.C" "C.C"
C. The name of the file.
B. "B.C" "C.C"
D. The last character in the file.
C. "A.C"

D. Error in fclose()
4. Which of the following operations can be
performed on the file "NOTES.TXT" using
the below code? 7. On executing the below program what will be
FILE *fp; the contents of 'target.txt' file if the source file
fp = fopen("NOTES.TXT", "r+"); contains a line "To err is human"?
#include<stdio.h>
A. Reading

B. Writing int main()


{
C. Appending int i, fss;
char ch, source[20] = "source.txt",
D. Read and Write target[20]="target.txt", t;
FILE *fs, *ft;
5. To print out a and b given below, which of the fs = fopen(source, "r");
following printf()statement will you use? ft = fopen(target, "w");
#include<stdio.h> while(1)
{
float a=3.14; ch=getc(fs);
double b=3.14; if(ch==EOF)
A. printf("%f %lf", a, b); break;

5
DIT, Pimpri Department of Electrical Engineering

else t = fileno(fp);
{ printf("%d\n", t);
fseek(fs, 4L, SEEK_CUR); return 0;
fputc(ch, ft); }
} A. size of "DUMMY.C" file
}
return 0; The handle associated with
B.
} "DUMMY.C" file
A. r n C. Garbage value
B. Trh D. Error in fileno()
C. err
Bitwise Operators
D. None of above
1. In which numbering system can the binary
8. To scan a and b given below, which of the number 1011011111000101 be easily
following scanf() statement will you use? converted to?
#include<stdio.h>
A. Decimal system
float a; B. Hexadecimal system
double b;
C. Octal system
A. scanf("%f %f", &a, &b);
D. No need to convert
B. scanf("%Lf %Lf", &a, &b);

C. scanf("%f %Lf", &a, &b); 2. Which bitwise operator is suitable for turning
off a particular bit in a number?
D. scanf("%f %lf", &a, &b);
A. && operator
9. Out of fgets() and gets() which function is safe B. & operator
to use?
C. || operator
A. gets()
D. ! operator
B. fgets()

3. Which bitwise operator is suitable for turning


10. Consider the following program and what will
on a particular bit in a number?
be content of t?
#include<stdio.h> A. && operator

B. & operator
int main()
{ C. || operator
FILE *fp;
int t; D. | operator
fp = fopen("DUMMY.C", "w");

6
DIT, Pimpri Department of Electrical Engineering

4. Which bitwise operator is suitable for printf("%s %d %f", e1.name, e1.age,


checking whether a particular bit is on or off? e1.salary);
A. && operator return 0;
}
B. & operator A. Error: RValue required
C. || operator Error: cannot convert from 'const int *'
B.
D. ! operator to 'int *const'

C. Error: LValue required in strcpy

D. No error
Constant

1. What will be the output of the program? 3. What will be the output of the program?
#include<stdio.h> #include<stdio.h>
int fun(int **ptr);
int main()
{ int main()
int y=128; {
const int x=y; int i=10;
printf("%d\n", x); const int *ptr = &i;
return 0; fun(&ptr);
} return 0;
}
A. 128
int fun(int **ptr)
B. Garbage value {
int j = 223;
C. Error int *temp = &j;
printf("Before changing ptr = %5x\n", *ptr);
D. 0
const *ptr = temp;
printf("After changing ptr = %5x\n", *ptr);
2. What will be the output of the program? return 0;
#include<stdio.h> }
#include<stdlib.h>
Address of i
A.
Address of j
union employee
{ 10
char name[15]; B.
223
int age;
float salary; Error: cannot convert parameter 1 from
C.
}; 'const int **' to 'int **'
const union employee e1;
D. Garbage value
int main()
{ 4. What will be the output of the program?
strcpy(e1.name, "K"); #include<stdio.h>
7
DIT, Pimpri Department of Electrical Engineering

{
int main() const char *s = "";
{ char str[] = "Hello";
const int x=5; s = str;
const int *ptrx; while(*s)
ptrx = &x; printf("%c", *s++);
*ptrx = 10;
printf("%d\n", x); return 0;
return 0; }
} A. Error
A. 5
B. H
B. 10
C. Hello
C. Error
D. Hel
D. Garbage value
7. What will be the output of the program?
5. What will be the output of the program in #include<stdio.h>
TurboC? int get();
#include<stdio.h>
int fun(int **ptr); int main()
{
int main() const int x = get();
{ printf("%d", x);
int i=10, j=20; return 0;
const int *ptr = &i; }
printf(" i = %5X", ptr); int get()
printf(" ptr = %d", *ptr); {
ptr = &j; return 20;
printf(" j = %5X", ptr); }
printf(" ptr = %d", *ptr); A. Garbage value
return 0;
} B. Error
A. i= FFE2 ptr=12 j=FFE4 ptr=24 C. 20
B. i= FFE4 ptr=10 j=FFE2 ptr=20 D. 0
C. i= FFE0 ptr=20 j=FFE1 ptr=30
8. What will be the output of the program (in
D. Garbage value Turbo C)?
#include<stdio.h>
6. What will be the output of the program?
#include<stdio.h> int fun(int *f)
{
int main() *f = 10;
8
DIT, Pimpri Department of Electrical Engineering

return 0; const int d = 34;


} printf("%d, %d\n", c, d);
int main() return 0;
{ }
const int arr[5] = {1, 2, 3, 4, 5}; A. Error
printf("Before modification arr[3] = %d",
arr[3]); B. -11, 34
fun(&arr[3]);
printf("\nAfter modification arr[3] = %d", C. 11, 34
arr[3]); D. None of these
return 0;
}
Before modification arr[3] = 4 Answer:
A.
After modification arr[3] = 10 Declarations
Error: cannot convert parameter 1 Q1 C Q2 B Q3 D Q4 B
B. Q5 A Q6 B Q7 B Q8 A
from const int * to int *
Q9 C Q10 A Q11 B
C. Error: Invalid parameter
Explanation:
Before modification arr[3] = 4
D. Q1.
After modification arr[3] = 4 fmod(x,y) - Calculates x modulo y, the remainder
of x/y.
9. What will be the output of the program? This function is the same as the modulus operator.
#include<stdio.h> But fmod() performs floating point divisions.

int main() Q2.


External Linkage-> means global, non-static
{
variables and functions.
const int i=0;
Internal Linkage-> means static variables and
printf("%d\n", i++); functions with file scope.
return 0; None Linkage-> means Local variables.
}
A. 10 Q3.
extern int fun(); declaration in C is to indicate
B. 11 the existence of a global function and it is defined
externally to the current module or in another file.
C. No output int fun(); declaration in C is to indicate the
existence of a function inside the current module or
in the same file.
D. Error: ++needs a value
Q7
10. What will be the output of the program?
C data types classification are
#include<stdio.h>
1. Primary data types
int main() 1. int
{ 2. char
const c = -11;
9
DIT, Pimpri Department of Electrical Engineering

3. float
4. double Q3.
A call stack or function stack is used for
5. void
several related purposes, but the main reason for
2. Secondary data types (or) User-defined data having one is to keep track of the point to which
type each active subroutine should return control when it
finishes executing.
1. Array A stack overflow occurs when too much memory
2. Pointer is used on the call stack.
Here function main() is called repeatedly and its
3. Structure return address is stored in the stack. After stack
memory is full. It shows stack overflow error.
4. Union
5. Enum Q6.
So, clearly long int l = 2.35; is not User-
Yes. If a function contains two return statements
defined data type.
(i.e.long int l = 2.35; is the answer.) successively, the compiler will generate
"Unreachable code" warnings.

Q8 Q7.
No, C can accept upto 127 maximum number of
Declaring is the way a programmer tells the
compiler to expect a particular type, be it a variable, arguments in a function.
class/struct/union type, a function type (prototype)
or a particular object instance. (ie. extern int i) Q8.
Declaration never reserves any space for the Yes, It will return the value 20*20 = 400
variable or instance in the program's memory; it
simply a "hint" to the compiler that a use of the Expression:
variable or instance is expected in the program. This
hinting is technically called "forward reference". Q1 A Q2 D Q3 C Q4 C
Q5 D Q6 A Q7 B Q8 B
Q9 B Q10 A
Q9.
extern int x; - is an external variable declaration.
Explanations:
double pow(double, double); - is a function Q1
prototype declaration. Left associativity

Therefore, 1 and 3 are declarations. 2 is definition. Q2.

Q10 Simply called as BODMAS (Bracket of Division,


Multiplication, Addition and Subtraction).
- During declaration we tell the datatype of the How Do I Remember ? BODMAS !
Variable.
- During definition the value is initialized. • B - Brackets first
• O - Orders (ie Powers and Square Roots, etc.)
• DM - Division and Multiplication (left-to-right)
Functions: • AS - Addition and Subtraction (left-to-right)

Q1 D Q2 C Q3 D Q4 B Q3.
Q5 B Q6 A Q7 B Q8 A
Option A: assignment statements are always return
in paranthesis in the case of conditional operator. It
Explanations: should be a>b? (c=30):(c=40);

10
DIT, Pimpri Department of Electrical Engineering

Option B: it is syntatically wrong. The fp is a structure which contains a char pointer


Option D: syntatically wrong, it should be return(a>b which points to the first character of a file.
? a:b);
Option C: it uses nested conditional operator, this is
Q4.
logic for finding greatest number out of three r+ Open an existing file for update (reading and
numbers. writing).

Q4.
Here, Multiplication will happen before the addition, Q9.
but in which order the functions would be called is Because, In fgets() we can specify the size of the
undefined. In an arithmetic expression the buffer into which the string supplied will be stored.
parenthesis tell the compiler which operands go with
which operators but do not force the compiler to Q10.
evaluate everything within the parenthesis first. fp = fopen("DUMMY.C", "w"); A file DUMMY.C
is opened in write mode and returns the file pointer
to fp
Q5.
t = fileno(fp); returns the handle for
An operation with only one operand is called unary the fp stream and it stored in the variable t
operation.
printf("%d\n", t); It prints the handle number.
Unary operators:
! Logical NOT operator.
~ bitwise NOT operator.
sizeof Size-of operator. Bitwise Operations:
&& Logical AND is a logical operator. Q1 B Q2 B Q3 D Q4 B
Therefore, 1, 2, 3 are unary operators.
Q6. Constant:
2. Arithmetic operators: *, /, %, +, -
1. Relational operators: >, <, >=, <=, ==, != Q1 Q2 Q3 Q4
3. Logical operators : !, &&, || Q5 Q6 Q7 Q8
4. Assignment operators: = Q9 Q10

Q10. Explanations:
Yes, Each and every operator has an associativity.
Q4
The associativity (or fixity) of an operator is a
Step 1: const int x=5; The constant
property that determines how operators of the same
variable x is declared as an integer data type and
precedence are grouped in the absence of
parentheses. Operators may be left-associative, initialized with value '5'.
right-associative or non-associative. Step 2: const int *ptrx; The constant
variable ptrx is declared as an integer pointer.
Step 3: ptrx = &x; The address of the constant
Input/Output: variable x is assigned to integer pointer variable
ptrx.
Step 4: *ptrx = 10; Here we are indirectly trying
Q1 C Q2 A Q3 B Q4 D to change the value of the constant vaiable x. This
Q5 A Q6 D Q7 B Q8 D will result in an error.
To change the value of const variable x we have to
Q9 B Q10 B
use *(int *)&x = 10;

Explanations: Q6.
Step 1: const char *s = ""; The constant
Q3. variable s is declared as an pointer to an array of
characters type and initialized with an empty string.

11
DIT, Pimpri Department of Electrical Engineering

Step 2: char str[] = "Hello"; The Step 1: const int i=0; The constant
variable str is declared as an array of charactrers variable 'i' is declared as an integer and initialized
type and initialized with a string "Hello". with value of '0'(zero).
Step 3: s = str; The value of the variable str is Step 2: printf("%d\n", i++); Here the
assigned to the variable s. Therefore str contains variable 'i' is increemented by 1(one). This will
the text "Hello". create an error "Cannot modify a const object".
Step 4: while(*s){ printf("%c", *s++); Because, we cannot modify a const variable.
} Here the while loop got executed untill the value of
the variable s is available and it prints the each Q10.
character of the variable s. Step 1: const c = -11; The constant
variable 'c' is declared and initialized to value "-
Hence the output of the program is "Hello".
11".
Step 2: const int d = 34; The constant
variable 'd' is declared as an integer and initialized
Q7. to value '34'.
Step 1: int get(); This is the function prototype Step 3: printf("%d, %d\n", c, d); The value
for the funtion get(), it tells the compiler returns an of the variable 'c'and 'd' are printed.
integer value and accept no parameters.
Step 2: const int x = get(); The constant Hence the output of the program is -11, 34
variable x is declared as an integer data type and
initialized with the value "20".
The function get() returns the value "20".
Step 3: printf("%d", x); It prints the value of
the variable x.
Hence the output of the program is "20".

Q8.
Step 1: const int arr[5] = {1, 2, 3, 4,
5}; The constant variable arr is declared as an
integer array and initialized to
arr[0] = 1, arr[1] = 2, arr[2] = 3,
arr[3] = 4, arr[4] = 5
Step 2: printf("Before modification arr[3]
= %d", arr[3]);It prints the value of arr[3] (ie.
4).
Step 3: fun(&arr[3]); The memory location of
the arr[3] is passed to fun() and arr[3] value is
modified to 10.
A const variable can be indirectly modified by a
pointer.
Step 4: printf("After modification arr[3]
= %d", arr[3]);It prints the value of arr[3] (ie.
10).
Hence the output of the program is
Before modification arr[3] = 4
After modification arr[3] = 10

Q9.
This program will show an error "Cannot modify a
const object".

12

Das könnte Ihnen auch gefallen