Sei sulla pagina 1di 58

MCQ

Topics :

• Loops
• Variables
• Command line Arguments
• Data Structure and Algorithm
• Array
• Data Types
• Functions
• Scope
• Recursion & Iteration
• File Handling
• Trees
• Graphs
• Linked List
• Stack & Queues
• Registers
Today’s Topic

• Loops
• Variables
• Command line Arguments
• Data Structure and Algorithm
• Array
How do you print ‘hello’ without “;”

#include <stdio.h>

int main(){
if(printf(“hello”)){
}
}
QUESTION: 01

void main()
{ OPTIONS:
for(int i=0; i<=3; i++)
A. 0 1 2
{
if(i == 2) B. No output
break;
C. 0 1 2 3
printf(“%d”,i);
} D. 0 1
}
OUTPUT: D
QUESTION: 02

void main ()
OPTIONS:
{ A. Garbage Value
B. 5
int arr[10] = { 1,2,3,4,5};
C. 6
printf(“%d”,arr[5]); D. 0
}

OUTPUT: D
QUESTION: 03

#include <stdio.h>
int main() OPTIONS:
{ A. Yes
int i = 1; B. No
if (i++ && (i == 1)) C. Depends on the compiler
printf("Yes\n"); D. Depends on the standard
else
printf("No\n");
}
OUTPUT: B
QUESTION: 05

int main( )
OPTIONS:
{
A. 5
int i;
B. 0 1 2 3 4 5 6 7 8 9
for(i=0; i< 10; i++){
C. 0 1 2 3 4 6 7 8 9
if(i == 5)
D. 0 1 2 3 4 5
continue;
OUTPUT: C
printf(“%d”,i);
}
return 0; }
QUESTION: 06

Activity Selection problem is an example of

OPTIONS:
A. Dynamic Algorithm
B. Greedy Algorithm
C. Recursive Approach
D. Divide & Conquer

OUTPUT: B
QUESTION: 07

#include <stdio.h>
OPTIONS:
int main(){
A. 0, 4, 2, 1
int a = -1, b = 4, c = 1, d;
B. 0, 5, 2, 1
d = ++a && ++b || ++c;
C. -1, 4, 1, 1
printf("%d, %d, %d, %d\n", a, b, c, d);
D. 0, 5, 1, 0
return 0;
}
OUTPUT: A
QUESTION: 08

public class loop{


public static void main(String[] args){
OPTIONS:
int a=5, b;
A. 012345
for(b=0; b<=a; b++);
B. 0
{
C. Compiler error
System.out.print(b);
D. 6
}
}
}

OUTPUT: D
QUESTION: 09

public class loop{


public static void main(String[] args){ OPTIONS:
int a=012, b; A. 012345678910
for(b=0; b<=a; b++); B. 0
{ C. Compiler error
System.out.print(b); D. 11
}
}
}

OUTPUT: D
QUESTION: 10

The minimum number of edges required to convert a cyclid graph of n


vertices to a spanning tree is

OPTIONS:
A. n
B. n - 1
C. n + 1
D. 2n
OUTPUT: B
QUESTION: 12

The θ notation in asymptotic evaluation represents −

OPTIONS:
A. Best case
B. Average case
C. Worst case
D. NULL case

OUTPUT: B
QUESTION: 13

public class loop{


public static void main(String[] args){ OPTIONS:
int i=0; A. Infinite loop
for(; i<4; i++) B. True false false false
{ C. True true false false
System.out.println(i<2); D. Compiler error
}
}}

OUTPUT: C
POINTERS

#include "stdio.h“ **pp = 10;

int main(void) { printf("%d\n",i);

int i = 5; i = 15;

int *p = &i; printf("%d\n",**pp);

int **pp = &p; printf("%x\n",**pp);

printf("%x\n",p); }

printf("%x\n",pp);
printf("%x\n",*pp);
printf("%x\n",**pp);
QUESTION: 15

#include <stdio.h>
int main(void) { OPTIONS:
int i = 10; A. 90
int j = 20; B. 10
int *p = &i; C. Error
int **pp = &p; D. 20
**pp = 90;
p = &j;
printf("%d", **pp);
return 0; OUTPUT: D
}
QUESTION: 16

#include <stdio.h>
int main(void) { OPTIONS:
int i = 10; A. 90
int j = 20; B. 10
int *p = &i; C. Error
int **pp = &p; D. 20
**pp = 90;
p = NULL;
printf("%d", **pp);
return 0; OUTPUT: C
}
QUESTION: 17

#include<stdio.h>
int main() {
OPTIONS:
int a = 6; A. 6
int *p = &a; B. 7
(*p)++; C. 0
printf("%d", *p); D. error
return 0;
}
OUTPUT: B
QUESTION: 18

#include<stdio.h>
int main() { OPTIONS:
int a = 6; A. 7 10
int *p = &a; B. 7 11
(*p)++; C. 10 10
int j = 10; D. 6 11
p = &j;
printf("%d %d", a, *p);
return 0;
} OUTPUT: A
QUESTION: 19

class jump_statments else if (y == 8)


{ break;
public static void main(String args[]) else
{ System.out.print(y + " ");
int x = 2; }
int y = 0; }
for ( ; y < 10; ++y) }
{
OPTIONS:
if (y % x == 0)
a) 1 3 5 7
continue;
b) 2 4 6 8
c) 1 3 5 7 9
d) 1 2 3 4 5 6 7 8 9
OUTPUT: C
Let’s CODE
Program: 01

Given an array of positive integers, rearrange the array alternately i.e first element
should be maximum value, second minimum value, third second max, fourth second
min and so on.
Example:
Input Array (First number is Number of elements)
123456789
Modified Array (Output)
9 1 8 2 7 3 6 4 5 
Program: 01

Constraints:
N>0
Sample Input
12346
Sample Output
61423
SOLUTION

void RearrangeMaxMin(int array[], int N){ int PrintArray(int array[],int N)


int temp[N]; {
for (int i=0; i<N; i++) { for (int i=0; i<N; i++)
temp[i] = 0; { cout << array[i] << " ";
} }
int low=0, high=N-1; }
int X = true; int main()
for (int i=0; i<N; i++) {
{ if(X) { int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
temp[i] = array[high--]; int N = sizeof(array)/sizeof(array[0]);
} cout << "Input array\n";
else { PrintArray(array,N); RearrangeMaxMin(array,N);
temp[i] = array[low++]; cout << "\nOutput array\n"; PrintArray(array,N);
} X = !X; } return 0;
for (int i=0; i<N; i++) }
{
array[i] = temp[i]; }}
QUESTION: 42

#include <stdio.h>
int main() OPTIONS:
{ A. -1 -3
int i=0; B. 1 2 3
for(i++; i<4;i++) C. -2 -3 -4
{ D. Compilation error
printf("%d",~i);
}
}

OUTPUT: C
QUESTION: 24

int main(){
int i = 0; OPTIONS:
while (i < 2){ A. In while loop After loop
if (i == 1) B. After loop
break; C. In while loop In while loop After loop
i++; D. In while loop
if (i == 1)
continue;
printf("In while loop");
} OUTPUT: B
printf("After loop");
}
QUESTION: 25

Consider the given graph.


What is the weight of the minimum spanning tree
using the Prim’s algorithm , starting from vertex a?
a) 23
b) 28
c) 27
d) 11
ANSWER: C
QUESTION: 26

What will be the result?


int i = 10; OPTIONS:
while(i++ <= 10) A. 10
{ B. 11
i++; C. 12
} D. 13
printf(“%d”,i); E. Line 5 will be never reached.

OUTPUT: D
QUESTION: 30

void main ()
{ OPTIONS:
int a[5] = {5,1,15,20,25}; A. 3,2,15
int i,j,m; B. 2,3,20
i=++a[1]; C. 2,1,15
j=a[1]++; D. 1,2,5
m=a[i++];
printf("%d %d %d",i,j,m);
}
OUTPUT: A
QUESTION: 28

int main()
{ OPTIONS:
int val = 1; A. 25
do { B. 50
val++; C. 12
++val; D. 4
} while (val++ > 25);
printf("%d\n", val);
return 0;
} OUTPUT: D
QUESTION: 22

What is the output of this program?


class comma_operator OPTIONS:
{ A. 5
public static void main(String args[]) B. 6
{ C. 4
int sum = 0; D. Compilation error
for (int i = 0, j = 0; i < 5 && j < 5; ++i, j = i
+ 1)
{
sum += i; OUTPUT: B
}
System.out.println(sum);
} }
QUESTION: 23

Which of the following is true?


a) Prim’s algorithm initialises with a vertex
b) Prim’s algorithm initialises with a edge
c) Prim’s algorithm initialises with a vertex which has smallest edge
d) Prim’s algorithm initialises with a forest

ANSWER: A
QUESTION: 29

int i = 5;
if( i ){ OPTIONS:
printf("Hello, World! \n");
A. Hello, World!
}
else B. Bye, World!
{
C. error
printf(“Bye, World! \n");
} D. No output

OUTPUT: A
QUESTION: 21

Project scheduling is an example of

OPTIONS:
A. greedy programming
B. dynamic programming
C. divide and conquer
D. none of the above

OUTPUT: B
QUESTION: 31

void main ()
{ OPTIONS:
char str1[ ] =“abcd”; A. Equal
B. Unequal
char str2[ ] = “abcd”; C. Error
if(str1 == str2) D. None of these

printf(“Equal”);
else
printf(“unequal”); OUTPUT: B

}
QUESTION: 32

#include <stdio.h>
int main() OPTIONS:
{ A. 0
int x = 2, y = 0; B. 1
int z = x && y = 1; C. Compile time error
printf("%d\n", z); D. 2
}

OUTPUT: C
QUESTION: 33

int i = 0;
if( i ){ OPTIONS:
printf("Hello, World! \n");
A. Hello, World!
}
else{ B. Bye, World!
printf(“Bye, World! \n");
C. error
}
D. No output

OUTPUT: B
QUESTION: 34

int i = 5;
if( i == 15 ); OPTIONS:
{
A. Hello, World!
printf("Hello, World! \n");
} B. Bye, World!
C. error
D. No output

OUTPUT: A
QUESTION: 35

What is the output of this C code?


#include <stdio.h> OPTIONS:
int main() A. 2 1
{ B. 1 1
int a = 1, b = 2; C. 3 -1
a += b -= a; D. 1 2
printf("%d %d", a, b);
}

OUTPUT: A
QUESTION: 36

Which of the following for loops will be an infinite loop?


A. for(; ;)
B. for(i=0 ; i<1; i--)
C. for(i=0; ; i++)
D. All of the above

ANSWER: D
QUESTION: 39

Find the output of below OPTIONS:


int a=5,b=18; A. 3
if((a=3)==b) B. 18
{ System.out.println(++a);} C. 19
else D. 6
{ System.out.println(++b); }

OUTPUT: C
QUESTION: 40

#include <stdio.h>
int main() OPTIONS:
{ A. 2
int y = 2; B. 12
int z = (y = 10)+y; C. 20
printf("%d\n", z); D. error
}

OUTPUT: C
QUESTION: 41

#include <stdio.h>
int main() OPTIONS:
{ A. 2
int y = 2; B. 12
int z = y+(y=10); C. 20
printf("%d\n", z); D. error
}

OUTPUT: C
QUESTION: 42

Which one of the following is a valid statement?


A. char[] c = new char();
B. char[] c = new char[5];
C. char[] c = new char(4);
D. char[] c = new char[];

OUTPUT: B
QUESTION: 43

What is returned from mystery when it is A. 17.5


passed {10, 30, 30, 60}? B. 30.0
public static double mystery(int[] arr) C. 130
{ D. 32
double output = 0; E. 32.5
for (int i = 0; i < arr.length; i++)
{
output = output + arr[i];
}
return output / arr.length; OUTPUT: E
}
QUESTION: 44

import java.util.Arrays; OPTIONS:


class Test A. Same
{ B. Not Same
    public static void main (String[] args)
    {
        int arr1[] = {1, 2, 3};
        int arr2[] = {1, 2, 3};
        if (Arrays.equals(arr1, arr2))
            System.out.println("Same");
        else OUTPUT: A
            System.out.println("Not same");
    }
}
QUESTION: 45

class Test OPTIONS:


{ A. Same
    public static void main (String[] args) B. Not Same
    {
        int arr1[] = {1, 2, 3};
        int arr2[] = {1, 2, 3};
        if (arr1.equals(arr2))
            System.out.println("Same");
        else
            System.out.println("Not same"); OUTPUT: B
    }
}
QUESTION: 42

What will be the output of the following program if argument passed to command lines
are : prog 1 4 2
#include<stdio.h>
int main(int argc, char *argv[])
{ OPTIONS:
    int j; a) Error
    j = argv[1] + argv[2] – argv[3]; b)3
    printf(“%d”, j); c)Garbage Value
    return 0; d)None of these
} OUTPUT: a
QUESTION: 42

What argv[0] and argv[1] denote in Command line Arguments ?

a) Pointers to first two command line argument supplied.


b) File Name and pointer to first command line argument supplied.
c) Program Name and Pointer to the 1st argument.
d) None of these.

OUTPUT: b
QUESTION: 38

int main()
default: printf("Choice other than 1,
{
2 and 3");
   int x = 2;
                break;  
   switch (x)
   }
   {
   return 0;
       case 1: printf("Choice is 1");

               break;
       case 2: printf("Choice is 2");
                break; OUTPUT: Choice is 2
       case 3: printf("Choice is 3");
               break;
QUESTION: 42

What is the first argument of command line ?

a)File Name
b)Program Designation
c)argument passed by user
d)Program Name

OUTPUT: A
QUESTION: 37

#include <stdio.h>
void main(){ OPTIONS:
int h = 8; A. 9
int b = h++ + h++ + h++; B. 10
printf("%d\n", h); C. 12
} D. 11

OUTPUT: D
Program: 02
Given n doors and n persons. The doors are numbered from 1 to n and persons are
given id’s numbered from 1 to n. Each door can have only two statuses ie open (1)
or closed (0). Initially, all the doors have status closed.
Find the final status of the door which is mentioned, when all the persons have
changed the status of the doors of which they are authorized. i.e. if status " open "
then change the status to closed and vice versa. A person with id ‘i’ is authorized to
change the status of the door numbered ‘j’ if ‘j’ is a multiple of ‘i’.

● Note: A person has to change the current status of all the doors for which he is
authorized exactly open
Program: 02
import java.util.Scanner; for(int i=2; i<n; i++)
public class Toggle { {
public static void main(String[] args) { if(n%i==0)
{
System.out.println("enter the number");
fact++;
Scanner sc=new Scanner(System.in); }
int n=sc.nextInt(); }
if( n==1) if( fact%2==0)
{ {
System.out.println("open"); System.out.println("closed");
} }
else
else{
System.out.println("open");
int fact=2; }}
}
THANK YOU

Potrebbero piacerti anche