0% found this document useful (0 votes)
2 views14 pages

C and JAVA Programming Aptitude Test-2

The document contains a series of programming aptitude test questions focused on C and Java programming languages. Each question presents a code snippet and multiple-choice answers regarding the output or behavior of the code. The questions cover various programming concepts including pointers, conditional statements, and class behavior.

Uploaded by

selssels2341
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

C and JAVA Programming Aptitude Test-2

The document contains a series of programming aptitude test questions focused on C and Java programming languages. Each question presents a code snippet and multiple-choice answers regarding the output or behavior of the code. The questions cover various programming concepts including pointers, conditional statements, and class behavior.

Uploaded by

selssels2341
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

C and JAVA Programming Aptitude Test-2

1. What will be the output of the following C code?

#include <stdio.h>
void main()
{
int x = 4;
int *p = &x;
int *k = p++;
int r = p - k;
printf("%d", r);
}

a) 4
b) 8
c) 1
d) Run time error

2. What will be the output of the following C code?

#include <stdio.h>
void main()
{
int a = 5, b = -7, c = 0, d;
d = ++a && ++b || ++c;
printf("\n%d%d%d%d", a, b, c, d);
}

a) 6 -6 0 0
b) 6 -5 0 1
c) -6 -6 0 1
d) 6 -6 0 1

3. What will be the output of the following C code?

#include <stdio.h>
void main()
{
int a = -5;
int k = (a++, ++a);
printf("%d\n", k);
}
a) -3
b) -5
c) 4
d) Undefined

4. What will be the output of the following C code?


#include <stdio.h>
int main()
{
int x = -2;
x = x >> 1;
printf("%d\n", x);
}
a) 1
b) -1
c) 2
d) Either -1 or 1

5. What will be the output of the following C code?

#include <stdio.h>
int main()
{
if (~0 == 1)
printf("yes\n");
else
printf("no\n");
}
a) yes
b) no
c) compile time error
d) undefined

6. What will be the output of the following C code?

#include <stdio.h>
int main()
{
int x = -2;
if (!0 == 1)
printf("yes\n");
else
printf("no\n");
}
a) yes
b) no
c) run time error
d) undefined

7. What will be the output of the following C code?

#include <stdio.h>
int main()
{
int y = 0;
if (1 |(y = 1))
printf("y is %d\n", y);
else
printf("%d\n", y);

}
a) y is 1
b) 1
c) run time error
d) undefined
8. What will be the output of the following C code?

#include <stdio.h>
int main()
{
int x = 2, y = 0;
int z = (y++) ? y == 1 && x : 0;
printf("%d\n", z);
return 0;
}
a) 0
b) 1
c) Undefined behaviour
d) Compile time error

9. What will be the output of the following C code?

#include <stdio.h>
int main()
{
int x = 1;
int y = x == 1 ? getchar(): 2;
printf("%d\n", y);
}
a) Compile time error
b) Whatever character getchar function returns
c) Ascii value of character getchar function returns
d) 2

10. What will be the output of the following C code?

#include <stdio.h>
int main()
{
int x = 1;
short int i = 2;
float f = 3;
if (sizeof((x == 2) ? f : i) == sizeof(float))
printf("float\n");
else if (sizeof((x == 2) ? f : i) == sizeof(short int))
printf("short int\n");
}
a) float
b) short int
c) Undefined behaviour
d) Compile time error

11. What will be the output of the following C code?

#include <stdio.h>
int main()
{
int a = 2;
int b = 0;
int y = (b == 0) ? a :(a > b) ? (b = 1): a;
printf("%d\n", y);
}
a) Compile time error
b) 1
c) 2
d) Undefined behaviour

12. What will be the output of the following C code?

#include <stdio.h>
void main()
{
int k = 8;
int m = 7;
k < m ? k++ : m = k;
printf("%d", k);
}

a) 7
b) 8
c) Compile time error
d) Run time error

13. What will be the output of the following C code?

#include <stdio.h>
void main()
{
int k = 8;
int m = 7;
k < m ? k = k + 1 : m = m + 1;
printf("%d", k);
}
a) Compile time error
b) 9
c) 8
d) Run time error

14. What will be the final value of c in the following C code snippet?
(Initial values: a = 1, b = 2, c = 1)

c += (-c) ? a : b;
a) Syntax Error
b) c = 1
c) c = 2
d) c = 3

15. What will be the output of the following C code?

#include <stdio.h>
void main()
{
int x = 1, y = 0, z = 5;
int a = x && y || z++;
printf("%d", z);
}
a) 6
b) 5
c) 0
d) Varies

16. What will be the output of the following C code?

#include <stdio.h>
int main()
{
int x = 1, y = 0, z = 3;
x > y ? printf("%d", z) : return z;
}
a) 3
b) 1
c) Compile time error
d) Run time error

17. What will be the output of the following C code?

#include <stdio.h>
void main()
{
int x = 0, y = 2, z = 3;
int a = x & y | z;
printf("%d", a);
}

a) 3
b) 0
c) 2
d) Run time error

18. What will be the final value of j in the following C code?

#include <stdio.h>
int main()
{
int i = 0, j = 0;
if (i && (j = i + 10))
//do something
;
}

a) 0
b) 10
c) Depends on the compiler
d) Depends on language standard

19. What will be the output of the following C code?

#include <stdio.h>
int main()
{
int i = 1;
if (i++ && (i == 1))
printf("Yes\n");
else
printf("No\n");
}
a) Yes
b) No
c) Depends on the compiler
d) Depends on the standard

20. What will be the output of the following C code?

#include <stdio.h>
int main()
{
int a = 10, b = 5, c = 3;
b != !a;
c = !!a;
printf("%d\t%d", b, c);
}
a) 5 1
b) 0 3
c) 5 3
d) 1 1

21. What will be the output of the program?

class SC2
{
public static void main(String [] args)
{
SC2 s = new SC2();
s.start();
}

void start()
{
int a = 3;
int b = 4;
System.out.print(" " + 7 + 2 + " ");
System.out.print(a + b);
System.out.print(" " + a + b + " ");
System.out.print(foo() + a + b + " ");
System.out.println(a + b + foo());
}

String foo()
{
return "foo";
}
}
A. 9 7 7 foo 7 7foo
B. 72 34 34 foo34 34foo
C. 9 7 7 foo34 34foo
D. 72 7 34 foo34 7foo
22. What will be the output of the program?

class Equals
{
public static void main(String [] args)
{
int x = 100;
double y = 100.1;
boolean b = (x = y); /* Line 7 */
System.out.println(b);
}
}
A. true
B. false
C. Compilation fails
D. An exception is thrown at runtime

23. What will be the output of the program?

class PassS
{
public static void main(String [] args)
{
PassS p = new PassS();
p.start();
}

void start()
{
String s1 = "slip";
String s2 = fix(s1);
System.out.println(s1 + " " + s2);
}

String fix(String s1)


{
s1 = s1 + "stream";
System.out.print(s1 + " ");
return "stream";
}
}
A. slip stream
B. slipstream stream
C. stream slip stream
D. slipstream slip stream

24. What will be the output of the program?

class Test
{
public static void main(String [] args)
{
Test p = new Test();
p.start();
}

void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}

boolean fix(boolean b1)


{
b1 = true;
return b1;
}
}
A. true true
B. false true
C. true false
D. false false

25. What will be the output of the program?

class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5; z++)
{
if (( ++x > 2 ) && (++y > 2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}
A. 5 2
B. 5 3
C. 6 3
D. 6 4

26. What will be the output of the program?

class PassA
{
public static void main(String [] args)
{
PassA p = new PassA();
p.start();
}

void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}
long [] fix(long [] a3)
{
a3[1] = 7;
return a3;
}
}
A. 12 15
B. 15 15
C. 3 4 5 3 7 5
D. 3 7 5 3 7 5

27. What will be the output of the program?

class Super
{
public int i = 0;

public Super(String text) /* Line 4 */


{
i = 1;
}
}

class Sub extends Super


{
public Sub(String text)
{
i = 2;
}

public static void main(String args[])


{
Sub sub = new Sub("Hello");
System.out.println(sub.i);
}
}
A. 0
B. 1
C. 2
D. Compilation fails.

28. What will be the output of the program?

public class Test


{
public int aMethod()
{
static int i = 0;
i++;
return i;
}
public static void main(String args[])
{
Test test = new Test();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}
A. 0
B. 1
C. 2
D. Compilation fails.

29. What will be the output of the program?

class Base
{
Base()
{
System.out.print("Base");
}
}
public class Alpha extends Base
{
public static void main(String[] args)
{
new Alpha(); /* Line 12 */
new Base(); /* Line 13 */
}
}
A. Base
B. BaseBase
C. Compilation fails
D. The code runs with no output

30. What will be the output of the program?

interface Count
{
short counter = 0;
void countUp();
}
public class TestCount implements Count
{
public static void main(String [] args)
{
TestCount t = new TestCount();
t.countUp();
}
public void countUp()
{
for (int x = 6; x>counter; x--, ++counter) /* Line 14 */
{
System.out.print(" " + counter);
}
}
}
A. 0 1 2
B. 1 2 3
C. 0 1 2 3
D. 1 2 3 4
E. Compilation fails
31. What will be the output of the program?

public class A
{
void A() /* Line 3 */
{
System.out.println("Class A");
}
public static void main(String[] args)
{
new A();
}
}
A. Class A
B. Compilation fails.
C. An exception is thrown at line 3.
D. The code executes with no output.

32. What will be the output of the program?

public class Test


{
public static void main(String args[])
{
class Foo
{
public int i = 3;
}
Object o = (Object)new Foo();
Foo foo = (Foo)o;
System.out.println("i = " + foo.i);
}
}
A. i = 3
B. Compilation fails.
C. i = 5
D. A ClassCastException will occur.

33. What will be the output of the program?

class A
{
final public int GetResult(int a, int b) { return 0; }
}
class B extends A
{
public int GetResult(int a, int b) {return 1; }
}
public class Test
{
public static void main(String args[])
{
B b = new B();
System.out.println("x = " + b.GetResult(0, 1));
}
}
A. x = 0
B. x = 1
C. Compilation fails.
D. An exception is thrown at runtime.

34. What will be the output of the program?

public class ArrayTest


{
public static void main(String[ ] args)
{
float f1[ ], f2[ ];
f1 = new float[10];
f2 = f1;
System.out.println("f2[0] = " + f2[0]);
}
}
A. It prints f2[0] = 0.0
B. It prints f2[0] = NaN
C. An error at f2 = f1; causes compile to fail.
D. It prints the garbage value.

35. Which of the following code fragments inserted, will allow to


compile?

public class Outer


{
public void someOuterMethod()
{
//Line 5
}
public class Inner { }

public static void main(String[] argv)


{
Outer ot = new Outer();
//Line 10
}
}

A. new Inner(); //At line 5


B. new Inner(); //At line 10
C. new ot.Inner(); //At line 10
D. new Outer.Inner(); //At line 10
36.

interface Base
{
boolean m1 ();
byte m2(short s);
}
which two code fragments will compile?

1. interface Base2 implements Base {}

2. abstract class Class2 extends Base


{ public boolean m1(){ return true; }}

3. abstract class Class2 implements Base {}

4. abstract class Class2 implements Base


{ public boolean m1(){ return (7 > 4); }}

5. abstract class Class2 implements Base


{ protected boolean m1(){ return (5 > 7) }}

A. 1 and 2
B. 2 and 3
C. 3 and 4
D. 1 and 5

37.Which three are valid method signatures in an interface?

private int getArea();


public float getVol(float x);
public void main(String [] args);
public static void main(String [] args);
boolean setFlag(Boolean [] test);

A. 1 and 2
B. 2, 3 and 5
C. 3, 4, and 5
D. 2 and 4

38. Which two of the following are legal declarations for nonnested
classes and interfaces?

1. final abstract class Test {}


2. public static interface Test {}
3. final public class Test {}
4. protected abstract class Test {}
5. protected interface Test {}
6. abstract public class Test {}

A. 1 and 4
B. 2 and 5
C. 3 and 6
D. 4 and 6
39. What will be the output of the program?

public class CommandArgsThree


{
public static void main(String [] args)
{
String [][] argCopy = new String[2][2];
int x;
argCopy[0] = args;
x = argCopy[0].length;
for (int y = 0; y < x; y++)
{
System.out.print(" " + argCopy[0][y]);
}
}
}

> java CommandArgsThree 1 2 3

A. 0 0
B. 1 2
C. 0 0 0
D. 1 2 3

40. What will be the output of the program ?

public class Test


{
public static void main(String [] args)
{
signed int x = 10;
for (int y=0; y<5; y++, x--)
System.out.print(x + ", ");
}
}
A. 10, 9, 8, 7, 6,
B. 9, 8, 7, 6, 5,
C. Compilation fails.
D. An exception is thrown at runtime.

You might also like