NPTEL Java MCQ Assignment Questions

100% found this document useful (1 vote)
2K views9 pages
This document contains 15 multiple choice questions about programming concepts in C and Java. The questions cover topics like data types, operators, functions, loops, arrays, strings and obj…

Uploaded by

Aniket Waghode
  • Question 1
  • Question 2
  • Assignment Introduction
  • Question 3
  • Question 4
  • Question 6
  • Question 5
  • Question 8
  • Question 7
  • Question 10
  • Question 9
  • Question 11
  • Question 13
  • Question 12
  • Question 14
  • Question 15

NPTEL Online Certification Courses

Indian Institute of Technology Kharagpur

PROGRAMMING IN JAVA
Assignment 0
TYPE OF QUESTION: MCQ
Number of questions: 15 Total mark: 15 × 1 = 15
______________________________________________________________________________

QUESTION 1:
In C programming, what is the use of %d format specifier in printf() function?

a. Prints double type value


b. Prints integer type value
c. Prints character type value
d. Prints string type value

Correct Answer: b

Detailed Solution:
The %d format specifier in printf() function is used to print integer type value.

____________________________________________________________________________

QUESTION 2:
Consider the following code and answer the questions.

int main()
{
int a[8] = {3, 1, 0, 5, 2, 7, 6, 4};
}

What is the value of a[a[a[3]]]? Select your correct answer from the following options.

A. Program is with a compile-time error


B. Program is with a run-time error
C. NULL
D. 4

Correct Answer: d

Detailed Solution:
Self-explanatory. Trace the result with execution on printing the desired value.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

____________________________________________________________________________

QUESTION 3:
Which of the following is not a valid identifier in C programming language?

a. 21Guns
b. No
c. yes
d. *p

Correct Answer: a

Detailed Solution:
In C programming language, the identifiers cannot start with a number. *p is valid where it
declares p as a pointer variable, such as int *p;

____________________________________________________________________________

QUESTION 4:
Consider the following function definition:
functionCheck( int a, int b)
{
return (( a < b ) ? 0 : ( a - b ));
}

Let x, y be any two non-negative integers. What does the function


functionCheck(x, functionCheck(x, y)) do? Select your correct answer
from the following choices.

a. Maximum of x and y.
b. Positive difference of x and y.
c. Sum of x and y.
d. Minimum of x and y.

Correct Answer: d
Detailed Solution:
This function functionCheck(x, functionCheck(x, y)) is to find the
minimum of x and y.
____________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 5:
What will be the output of the following code?

#include<stdio.h>

#define myfunc(i, j) i##j

int main()
{
int v1 = 40;
int v2 = 0;
printf("%d", myfunc(1, 1)*myfunc(v, 1));
return 0;
}

Select your correct answer from the following choices.

a. 40
b. 400
c. 440
d. 0

Correct Answer: c

Detailed Solution:
“myfunc(i, j) i##j ” meaning is concatenate second operand to first.
____________________________________________________________________________

QUESTION 6:
To store the string “Javajuly”, how many bytes that a C-compiler will take? Select your
correct answer from the following options.

a. 4 bytes.
b. 5 bytes.
c. 8 bytes.
d. 9 bytes.
Correct Answer: d

Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

A C string is as long as the number of characters between the beginning of the string and the
terminating null character. So it will take 9 bytes.
________________________________________________________________________

QUESTION 7:
Which of the following operator has the lowest priority ?

a. ++
b. +

c. %

d. ||

Correct Answer: d
Detailed Solution:
The || (Logical OR) has the lowest priority out of ++, + and %.
________________________________________________________________________

QUESTION 8:
In the declaration given below, what is “array”?

int array[20];

a. It is a keyword
b. It is a literal
c. It is an operator
d. It is a pointer

Correct Answer: d

Detailed Solution:
“array” is an identifier here. The name of the array is also a pointer to the starting of an array.

____________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 9:
What will be the output of the following C code?

#include <stdio.h>
int main()
{
void foo();
printf("1 ");
foo();
}
void foo()
{
printf("2 ");
}

Select your correct answer from the following choices.

A. 1 2
B. It will give compile-time error
C. 212
D. Depends on the compiler

Correct Answer: a

Detailed Solution:
Self-explanatory. Trace the program by execution.

____________________________________________________________________________

QUESTION 10:
Which of the following is a valid C expression?

A. int my_num = 100,000;


B. int my_num = 100000;
C. int my num = 1000;
D. int $my_num = 10000;
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

Correct Answer: b

Detailed Solution:
int my_num = 100,000;It is Invalid because it has a comma operator in integer number which is not
allowed in C.

int my num = 1000; It is Invalid because space is not allowed in the identifier name in C.

int $my_num = 10000; It is Invalid because The first letter of an identifier should be either a letter or
an underscore.

int my_num = 100000; This is the correct Expression.

____________________________________________________________________________

QUESTION 11:
Consider the following declarations:

float x=12.0, y=6.0;


int a=4, b=3;
float z;

What is the value of z computed in each of the following assignment statements?

i. z = x / y + a / b ;
ii. z = a / b * b / a * x ;
iii. z = (x / y) > 1 ? x : y ;
iv. z = (int) (x + b) / a % b ;

Select your correct answer from the following choices.

A. 3.0, 0.0, 12.0, 0.0


B. 0.0, 0.0, 0.0, 0.0
C. 0.0, 0.0, 12.0, 0.0
D. 3.0, 0.0, 12.0, 1.0

Correct Answer: a

Detailed Solution:
Self-explanatory. Trace the program by execution.

____________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

QUESTION 12:
Consider the following declarations:

#include<stdio.h>

void main()
{
int i, j = 10; y = 0;
for(i = 0; i < j; i++)
while(j) y += j--;
printf(%d”, y);
}

What will be the output of the program? Select your option from the following.

A. 100
B. 10 9 8 … 3 2 1
C. 10+9+8+ … +3+2+1
D. 0

Correct Answer: c

Detailed Solution:
Self-explanatory. Trace the program by execution.

____________________________________________________________________________

QUESTION 13:
A function foo(int) is defined as follows:

int foo(int i) {
int x=1, j = 1;

while (i) {
x *= j;
j++;
if(j<7) continue;
else break;
}
return x;
}
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

What will be the output of the program? Select your option from the following.

A. The program will be with compilation error


B. 6 5 4 3 2 1
C. 6+5+4+3+2+1
D. The program will be with an infinite loop and hence not return any value.

Correct Answer: B

Detailed Solution:
Self-explanatory. Trace the program by execution.

____________________________________________________________________________

QUESTION 14:

Which of the following statement(s) is/are NOT true.

A. Java is an object-oriented programming language


B. C is an object-oriented programming language
C. C++ is an object-oriented programming language
D. C# is an object-oriented programming language.

Correct Answer: B

Detailed Solution:
All others are object-oriented programming languages.

____________________________________________________________________________

QUESTION 15:

Which of the following is NOT possible in C programming without using specialized


libraries?

A. Dynamic memory allocation


B. Running multiple functions concurrently.
C. Recursive execution of a function.
D. Working with abstract data types.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur

Correct Answer: B

Detailed Solution:
C language does not support parallel execution of programs. Others are well-suited in C.

____________________________________________________________________________
******

Common questions

Powered by AI

i. 'z = x / y + a / b' results in 3.0 since float division x/y gives 2.0 added to integer division a/b which is 1. ii. 'z = a / b * b / a * x' simplifies to 0.0 due to integer zero results from division. iii. 'z = (x / y) > 1 ? x : y' evaluates to 12.0 as (x/y) is greater than 1. iv. 'z = (int)(x + b) / a % b' gives 0.0 from integer division and modulo.

The expression 'a[a[a[3]]]' in the given array int a[8] = {3, 1, 0, 5, 2, 7, 6, 4} evaluates to 4. This is calculated by first determining the value of a[3], which is 5. Therefore, the next step is to find a[5], which gives 7. Finally, accessing a[7] results in the value 4.

In C, a string requires an additional byte for the null terminator '\0', which signals the end of the string. Thus, the string "Javajuly" comprises 8 characters plus the null terminator, resulting in a total storage requirement of 9 bytes.

The macro 'myfunc(i, j) i##j' concatenates 'i' and 'j'. Thus, myfunc(1, 1) expands to '11', and myfunc(v, 1) becomes 'v1', where 'v1' is interpreted as the integer variable 'v1' initialized to 40. Therefore, the expression evaluates to 11 * 40, resulting in the output of 440.

'21Guns' is not a valid identifier in C programming because identifiers cannot start with a number. Valid identifiers must start with a letter or an underscore. The options 'No' and 'yes' are valid as they start with letters, while '*p' is considered valid for declaring a pointer variable with int *p.

In C, operator precedence determines the order in which operators are evaluated in expressions. The operators ++ (increment), + (addition), and % (modulo) have higher levels of precedence compared to the logical OR operator '||', which means that in composite expressions, '||' will be evaluated last unless parentheses enforce a different order.

In C, identifiers must be composed of letters, numbers, and underscores without leading digits. The expression 'int my_num = 100000;' is valid because it follows these guidelines. Spaces, commas, and characters like '$' violate identifier naming rules, thus leading to syntax errors.

The provided code '#include<stdio.h> int main() { int i, j = 10; y = 0; for(i = 0; i < j; i++) while(j) y += j--; printf("%d", y); }' has fundamental syntax errors. The error lies in undeclared variable 'y', leading to compilation errors, as every variable used needs a proper declaration.

Standard C does not include native facilities for parallel execution of functions or concurrent programming. Instead, it relies on libraries such as POSIX threads or OpenMP to enable concurrency, as the primary design of C was intended for low-level, sequential processing and system programming.

The function functionCheck(int a, int b) uses the conditional expression (a < b) ? 0 : (a - b) to determine its result. When evaluated as functionCheck(x, functionCheck(x, y)), the inner call functionCheck(x, y) returns 0 if x is less than y, otherwise x-y. The outer call functionCheck(x, 0) or functionCheck(x, x-y) uses the same logic; hence, it returns 0 or a positive difference, identifying the minimum of x and y.

NPTEL Online Certification Courses 
Indian Institute of Technology Kharagpur 
 
 
PROGRAMMING IN JAVA 
Assignment 0 
TYPE
NPTEL Online Certification Courses 
Indian Institute of Technology Kharagpur 
 
_________________________________________
NPTEL Online Certification Courses 
Indian Institute of Technology Kharagpur 
 
 
QUESTION 5: 
What will be the output of
NPTEL Online Certification Courses 
Indian Institute of Technology Kharagpur 
 
A C string is as long as the number of ch
NPTEL Online Certification Courses 
Indian Institute of Technology Kharagpur 
 
 
QUESTION 9: 
What will be the output of
NPTEL Online Certification Courses 
Indian Institute of Technology Kharagpur 
 
 
Correct Answer: b 
Detailed Solution:
NPTEL Online Certification Courses 
Indian Institute of Technology Kharagpur 
 
 
QUESTION 12: 
Consider the following de
NPTEL Online Certification Courses 
Indian Institute of Technology Kharagpur 
 
 
 
What will be the output of the progra
NPTEL Online Certification Courses 
Indian Institute of Technology Kharagpur 
 
 
Correct Answer: B 
Detailed Solution:

You might also like