0% found this document useful (0 votes)
5 views

Level1 Programming Test2

Uploaded by

singhkamal5858
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Level1 Programming Test2

Uploaded by

singhkamal5858
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

DIRECTIONS for the question: Mark the best option:

Which one of the choices given below would be printed when the following program is
executed?
#include
int a1[]= {6, 7, 8, 18, 34, 67};
int a2[] = {23, 56, 28, 29};
int a3[] = {-12, 27, -31};
int *x= {al, a2, a3};
void print(int *a[])
{
printf("%d,",a[0][2]);
printf("%d,”,*a[2]);
printf("%d,", ++a[0]);
printf(“%d,”,*(++a)[0]);
printf("%d\n",a[-1][+1]);
}
main()
{
print(x);
}
8, -12, 7, 23, 8
8, 8, 7, 23, 7
-12, -12, 27, -31, 23
-12, -12, 27, -31, 56

DIRECTIONS for the question: Mark the best option:


Consider the following C function in which size is the number of elements in the array E:
int MyX(int unsigned int size)
{
Int Y=0;
int Z;
int i, j, k;
for(i = 0; i< size; i++)
{
Y = Y + E[i];
for(i=0; i < size; i++)
for(j=0;j<size;j++)
{
Z=0;
For(k=i;k<=j;k++)
Z=Z+E[k];
If(Z>Y)
Y=Z;
}
return Y;
}

The value returned by the function Myx is the


maximum possible sum of elements in any sub-array of array E.
maximum element in any sub-array of array E.
sum of the maximum elements in all possible sub-arrays of array E.
the sum of all the elements in the array E.

DIRECTIONS for the question: Mark the best option:


Consider the following C program segment:
char p[20];
char *s = "string";
int length = strlen(s);
int i;
for (i = 0; i < length; i++)
p[i] = s[length - i];
printf("%s" ,p);

The output of the program is?


gnirts
string
gnirt
no output is printed

DIRECTIONS for the question: Mark the best option:


Consider the following program in C language:
#include
main ( )
{
int i;
int *pi= &i;
scanf("%d", pi);
printf("%d\n", i+5);
}

Which one of the following statements is TRUE?


Compilation fails.
Execution results in a run-time error.
On execution, the value printed is 5 more than the address of variable i.
On execution, the value printed is 5 more than the integer value entered.

DIRECTIONS for the question: Mark the best option:


Consider the C program given below:
# include
int main( )
{
int sum = 0, maxsum = 0, i, n=6;
int a() = {2, -2, -1, 3, 4, 2};
for(i = 0; i < n; i++)
{
If(i==0| | a[i]<0 | | a[i] < a [i - 1])
{
if(sum >maxsum)
maxsum = sum;
sum = (a[i] > 0) ? a[i] : 0;
}
else sum + = a[i];
if(sum >maxsum) maxsum = sum;
printf ("%d\n", maxsum);
}

What is the value printed out when this program is executed?


9
8
7
6

DIRECTIONS for the question: Mark the best option:


Let a be an array containing n integers in increasing order. The following algorithm
determines whether there are two distinct numbers in the array whose difference is a
specified number S > 0.
i=0
j=1
while (j < n)
{
if (E) j++;
else if (a[j] - a[i] = = S) break;
else i++;
}
if (j < n)
printf("yes");
else
printf(“no”)

Choose the correct expression for E.


a[ j ] - a[i] > S
a[ j ] -a[i] < S
a[i] – a[ j ] < S
a[i] – a[ j ] > S

DIRECTIONS for the question: Mark the best option:


Consider the following C-program
void foo (int n, int sum) {
int k = 0, , j = 0;
if (n ==0) return;
k=n%10; j=n/10;
sum = sum + k;
foo (j, sum);
printf ("%d,",k);
}
int main() {
int a = 2048, sum = 0;
foo(a, sum);
printf("%d\n", sum);
}

What does the above program print?


8, 4, 0, 2, 14
8, 4, 0, 2, 0
2, 0, 4, 8, 14
2, 0, 4, 8, 0

DIRECTIONS for the question: Mark the best option:


What will be the output of following c code?
#include
enum example {a = 1, b, c};
enum example example 1 = 2;
enum example answer()
{
return example 1 ;
int main()
{
(answer() == a)? printf("yes"): printf("no");
return 0;
}

Yes
No
2
Error
DIRECTIONS for the question: Mark the best option:
Consider the following C function
int f(int n)
{
static int i =1;
if(n >= 5)
return n;
n = n+i;
i++;
return f(n);
}

The value returned by f(1)is


5
6
7
8

DIRECTIONS for the question: Mark the best option:


What does the following program print?
#include
void f(int *p, int *q) {
p=q;
*p=2;
}
int i=0, j=1;

int main() {
f(&I, &j)
printf("%d %d\n", i,j);
return 0;
}

2 2
2 1
0 1
0 2

DIRECTIONS for the question: Mark the best option:


What is the output printed by the following program?
# include
int f(int n, int k) n
{
if (n= = 0)
return 0;
else if (n% 2)
return f(n/2, 2*k)+k;
else return f(n/2, 2*k) - k;
}
int main()
{
Printf(“%d”, f(20,1));
return 0;
}

5
8
9
20
DIRECTIONS for the question: Mark the best option:
Consider the following recursive C function that takes two arguments.
unsignedint foo(unsigned int n, unsigned int r)
{
if(n>O) return (n%r+foo(n/r,r));
else return 0;
}

What is the return value of the function foo when it is called as foo (513,2)?
9
8
5
2

DIRECTIONS for the question: Mark the best option:


What will be the output of the given c++ code?
#include
using namespace std;
class sample 1
{
float i, j;
};
class sample2
{
int x, y;
public:
sample2 (int a, int b)
{
x=a;
y=b;
}
Int result()
{
return x+y;
}
};
int main ()
{
Sample1 d;
sample2 * padd;
padd = (sample2*) &d;
cout<< result();
return 0;
}

Runtime error
20
Some random number
Both Option A and C

DIRECTIONS for the question: Mark the best option:


Consider the following C program:
#include
typedefstruct
{
char *a;
char *b; }t;
voidfl(t s); void £2(t *p); main()
{
Static t s={“A”, “B”};
printf("%s %s\n", s.a, s.b);
fl(s);
printf("%s %s\n", s.a, s.b);
f2(&s);}
voidf(t s)
{
s.a = "U";
s.b = "V"
printf(%s %s\n", s.a, s.b);
return;
}
void £2(t *p)
{
p - *a= “V”;
p -^ b = “W”;
printf("%s %s\n”,p -> a, p-> b);
return; }

What is the output generated by the program?


A B
U V
V W
V W

A B
U V
A B
V W
A B
U V
U V
V W

A B
U V
V W
U V

DIRECTIONS for the question: Mark the best option:


The following C function takes two ASCII strings and determines whether one is an anagram of
the other.
An anagram of a string s is a string obtained by permuting the letters in s.
int anagram (char *a, char *b)
{
int count [128], j;
for (j = 0; j < 128; j++) count[j] = 0;
j=0;
while (a[j] && b[j]) {
A;
B;
}
for (j = 0; j < 128; j++) if (count [j]) return 0;
return 1;
}

Choose the correct alternative for statements A and B


A : count [a[j]]++ and B : count[b[j]]–
A : count [a[j]]++ and B : count[b[j]]++
A : count [a[j++]] ++ and B : count[b[j]]–
A : count [a[j]]++and B : count[b[j++]]–

DIRECTIONS for the question: Mark the best option:


What will be the output of following c++ code?
#include <iostream>
using namespace std;
const int SIZE = 10;
class safe
{
private:
int arr[SlZE];
public:
safe()
{
register int i;
for (i = 0; i < SIZE; i++)
{
arr[i] = i;
}
}
int &operator[](int i)
{
if (i > SIZE)
{
cout << "Index out of bounds";
return arr[0];
}
return arr[i];
}
};
Int main()
{
safe A;
cout << A[5];
cout A[12];
return 0;
}

4
5
1 Index out of bounds 1
5 Index out of bounds 0

DIRECTIONS for the question: Mark the best option:


Consider the following C program
main()
{
int x, y, m, n;
scanf("%d%d", &x,&y);
/* Assume x > 0 and y >0 */
m=x; n=y;
while(m! = n)
{
if(m>n)
m= m-n
else
n= n-m
}
printf("%d" n);
}

The program computes


x + y using repeated subtraction
x mod y using repeated subtraction
the greatest common divisor of x and y
the least common multiple of x and y

DIRECTIONS for the question: Mark the best option:


Consider the following recursive C function that takes two arguments.
unsigned int foo(unsigned int n, unsigned int r)
{
if(n>0) return (n%r+foo(n/r,r));
else return 0;

What is the return value of the function foo when it is called as foo(345,10) ?
345
12
5
3

DIRECTIONS for the question: Mark the best option:


What is the output printed by the following C code?
# include
int main( )
{
char a[6] = "world";
int i, j;
for (i = 0, j = 5; i < j; a[i++] =a [j--]);
printf("%s\n",a);
}

dlrow
Null string
dlrld
worow
DIRECTIONS for the question: Mark the best option:
How many # 's do the following statements print?
for (int m=0, n=10; n-m > 5; ++m, - -n)
System.out.print(" # ");
0
1
2
3

DIRECTIONS for the question: Mark the best option:


Let Abe a square matrix of size n x n. Consider the following pseudocode. What is the expected
output?
c=100;
for i=1 to n do
for j=1 to n do
{
Temp = A[i][ j ]+C;
A[i][ j ]= A[ j ][ i ];
A[ j ][ i ]+= Temp -C;
}
for i=1 to n do
for j=1 to n do
Output (A[ i ] [ j ]);

The matrix A itself


Transpose of the matrix A
Adding 100 to the upper diagonal elements and subtracting 100 from lower diagonal elements
of A
None of the above

DIRECTIONS for the question: Mark the best option:


Which one of the choices given below would be printed when the following program is
executed?
#include
struct test
{
int i;
char *c;
}
st[] = {5, "become", 4, "better", 6, "jungle", 8, "ancestor", 7, "brother"};
main ()
{
struct test *p = st;
p+=1;
++p → c;
printf("%s," p++ → c);
printf("%c " *++p → c);
printf(“%d,”,p[0].i);
printf("%s \n", p → c);
}

jungle, n, 8, nclastor
etter, u, 6, ungle
cetter, k, 6, jungle
etter, u, 8, ncestor

DIRECTIONS for the question: Mark the best option:


Consider the C program below. What does it print?
# include
# define swap1 (a,b) tmp = a; a = b; b =tmp;
void swap2 (int a, int b){
int tmp;
tmp = a; a = b; b = tmp;
}
void swap3 (int*a, int*b){
int tmp;
tmp = *a; *a= *b; *b = tmp;
int main ()
{
int num1= 5, num2 = 4, tmp;
if (numl < num2) {swap1 (num1, num2);}
if (numl < num2) {swap2 (num1 + 1, num2);}
if (numl > = num2) {swap3 (&num1, &num2);}
printf ("%d, %d", num1, num2);
}

5, 5
5, 4
4, 5
4, 4

DIRECTIONS for the question: Mark the best option:


What is the output of the following program?
public class TestFirstApp {
public static void main{String[] args) {
int a = 3;
System.out.println (++a + ++a * ++a);
}
}

23
17
34
26

DIRECTIONS for the question: Mark the best option:


A C program is given below:
# include
int main ( )
{
int i, j;
char a [2] [3] = {{'a', 'b', 'c'}, {'d', 'e', 'f}}
char b [3] [2];
char *p = *b;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
*(p+2*j + i) = a[ i ] [ j ]
}
}
}
What should be the contents of the array b at the end of the program?
ab
cd
ef

ad
be
cf

ac
eb
cf

ae
dc
bf

DIRECTIONS for the question: Mark the best option:


What will be the output of following C language code?
#include
struct student {
char *name;
};
struct student s;
struct student fun(void) {
s.name = "newton";
printf("%s ", s.name);
s.name = "alan";
return s;
}
void main() {
struct student m = fun();
printf("%s ", m.name);
m.name = "turing";
printf("%s ", s.name);
}

alan newton newton


newton alan alan
alan alan newton
newton alan turing
Consider the program below in a hypothetical language which allows global variables and a
choice of call by reference or call by value methods of parameter passing:
Int i;
program main()
{ int j = 60;
i = 50;
call f(i, j);
print i, j;
}
procedure f(x, y)
{
i = 100;
x= 10;
y=y+i;
}

Which one of the following options represents the correct output of the program for the two
parameter passing mechanisms?
Call by value : i = 70, j = 10; Call by reference : i = 60, j = 70
Call by value : i = 50, j = 60; Call by reference : i = 50, j = 70
Call by value : i = 10, j = 70; Call by reference : i = 100, j = 60
Call by value : i = 100, j = 60; Call by reference : i = 10, j = 70

DIRECTIONS for the question: Mark the best option:


The following program
public class TestFirstApp {
public static void main(String[] args) {
int wer = 0x123;
System.out.println (wer);
}
}

outputs
123
0123
an unpredictable garbage value
291

DIRECTIONS for the question: Mark the best option:


What is the output of the following program?
#include
int funcf (int x);
int funcg (int y);
main()
{
int x = 5, y = 10, count;
for (count = 1; count < = 2; ++count)
{
y + = funcf (x) + funcg(x);
printf(%d",y);
}
}
funcf(int x)
{
int y;
y = funcg(g);
return (y);
}
funcg(int x)
{
static int y = 10;
y += I;
return (y + x);
}

43 80
42 74
33 37
32 32

DIRECTIONS for the question: Mark the best option:


Consider the C program given below. What does it print?
# include <stdio.h>
int main ()
{
int i, j;
int a [8] = {1, 2, 3, 4, 5, 6, 7, 8};
for (i = 0; i < 3; i++)
{
a[i] = a[i] + 1;
i++;
}
i--;
for (j = 7; j > 4; j--){
int i = j/2;
a[i] = a[i]-1;
printf ("%d, %d", i, a[i]);

2,3
2, 4
3, 2
3, 3

You might also like