0% found this document useful (0 votes)
3 views16 pages

C and JAVA Programming Aptitude Test-3

The document contains a series of programming aptitude test questions focused on C and Java programming concepts. It includes multiple-choice questions regarding code outputs, syntax errors, and behavior of various programming constructs. Each question is followed by four answer options, testing the reader's understanding of programming logic and language specifics.

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)
3 views16 pages

C and JAVA Programming Aptitude Test-3

The document contains a series of programming aptitude test questions focused on C and Java programming concepts. It includes multiple-choice questions regarding code outputs, syntax errors, and behavior of various programming constructs. Each question is followed by four answer options, testing the reader's understanding of programming logic and language specifics.

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/ 16

C and JAVA Programming Aptitude Test-3

1. What is the difference between the following 2 codes?

#include <stdio.h> //Program 1


int main()
{
int d, a = 1, b = 2;
d = a++ + ++b;
printf("%d %d %d", d, a, b);
}
#include <stdio.h> //Program 2
int main()
{
int d, a = 1, b = 2;
d = a++ +++b;
printf("%d %d %d", d, a, b);
}

a) No difference as space doesn’t make any difference, values of a, b, d


are same in both the case
b) Space does make a difference, values of a, b, d are different
c) Program 1 has syntax error, program 2 is not
d) Program 2 has syntax error, program 1 is not

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

#include <stdio.h>
int main()
{
int a[5] = {1, 2, 3, 4, 5};
int i;
for (i = 0; i < 5; i++)
if ((char)a[i] == '5')
printf("%d\n", a[i]);
else
printf("FAIL\n");
}

a) The compiler will flag an error


b) The program will compile and print the output 5
c) The program will compile and print the ASCII value of 5
d) The program will compile and print FAIL for 5 times

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

#include <stdio.h>
void main()
{
int x = 4, y, z;
y = --x;
z = x--;
printf("%d%d%d", x, y, z);
}
a) 3 2 3
b) 2 2 3
c) 3 2 2
d) 2 3 3

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

#include <stdio.h>
int main()
{
int y = 1;
if (y & (y = 2))
printf("true %d\n", y);
else
printf("false %d\n", y);

a) true 2
b) false 2
c) either true 2 or false 2
d) true 1

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

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

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

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

7. What will be the final values of a and c in the following C statement?


(Initial values: a = 2, c = 1)

c = (c) ? a = 0 : 2;
a) a = 0, c = 0;
b) a = 2, c = 2;
c) a = 2, c = 2;
d) a = 1, c = 2;

8. 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

9. What will be the final value of d in the following C code?

#include <stdio.h>
int main()
{
int a = 10, b = 5, c = 5;
int d;
d = b + c == a;
printf("%d", d);
}
a) Syntax error
b) 1
c) 5
d) 10

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

#include <stdio.h>
int main()
{
reverse(1);
}
void reverse(int i)
{
if (i > 5)
exit(0);
printf("%d\n", i);
return reverse(i++);
}
a) 1 2 3 4 5
b) 1 2 3 4
c) Compile time error
d) Stack overflow
11. What will be the output of the following C function?

#include <stdio.h>
void reverse(int i);
int main()
{
reverse(1);
}
void reverse(int i)
{
if (i > 5)
return ;
printf("%d ", i);
return reverse((i++, i));
}
a) 1 2 3 4 5
b) Segmentation fault
c) Compilation error
d) Undefined behaviour

12. What will be the final values of i and j in the following C code?

#include <stdio.h>
int x = 0;
int main()
{
int i = (f() + g()) || g();
int j = g() || (f() + g());
}
int f()
{
if (x == 0)
return x + 1;
else
return x - 1;
}
int g()
{
return x++;
}
a) i value is 1 and j value is 1
b) i value is 0 and j value is 0
c) i value is 1 and j value is undefined
d) i and j value are undefined

13. What will be the final values of i and j in the following C code?

#include <stdio.h>
int x = 0;
int main()
{
int i = (f() + g()) | g(); //bitwise or
int j = g() | (f() + g()); //bitwise or
}
int f()
{
if (x == 0)
return x + 1;
else
return x - 1;
}
int g()
{
return x++;
}
a) i value is 1 and j value is 1
b) i value is 0 and j value is 0
c) i value is 1 and j value is undefined
d) i and j value are undefined

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

#include <stdio.h>
int main()
{
int y = 2;
int z = y +(y = 10);
printf("%d\n", z);
}
a) 12
b) 20
c) 4
d) Either 12 or 20

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

#include <stdio.h>
int main()
{
char *p = NULL;
char *q = 0;
if (p)
printf(" p ");
else
printf("nullp");
if (q)
printf("q\n");
else
printf(" nullq\n");
}
a) nullp nullq
b) Depends on the compiler
c) x nullq where x can be p or nullp depending on the value of NULL
d) p q

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

#include <stdio.h>
int main()
{
int i = 10;
void *p = &i;
printf("%f\n", *(float*)p);
return 0;
}

a) Compile time error


b) Undefined behaviour
c) 10
d) 0.000000

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

#include <stdio.h>
int *f();
int main()
{
int *p = f();
printf("%d\n", *p);
}
int *f()
{
int j = 10;
return &j;
}
a) 10
b) Compile time error
c) Segmentation fault/runtime crash
d) Undefined behaviour

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

#include <stdio.h>
int main()
{
int *ptr, a = 10;
ptr = &a;
*ptr += 1;
printf("%d,%d/n", *ptr, a);
}
a) 10,10
b) 10,11
c) 11,10
d) 11,11

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

#include <stdio.h>
void foo(int*);
int main()
{
int i = 10, *p = &i;
foo(p++);
}
void foo(int *p)
{
printf("%d\n", *p);
}
a) 10
b) Some garbage value
c) Compile time error
d) Segmentation fault

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

#include <stdio.h>
int main()
{
int i = 97, *p = &i;
foo(&i);
printf("%d ", *p);
}
void foo(int *p)
{
int j = 2;
p = &j;
printf("%d ", *p);
}
a) 2 97
b) 2 2
c) Compile time error
d) Segmentation fault/code crash

21. What is the output of this program?

class access
{
public int x;
static int y;
void cal(int a, int b)
{
x += a ;
y += b;
}
}
class static_specifier
{
public static void main(String args[])
{
access obj1 = new access();
access obj2 = new access();
obj1.x = 0;
obj1.y = 0;
obj1.cal(1, 2);
obj2.x = 0;
obj2.cal(2, 3);
System.out.println(obj1.x + " " + obj2.y);
}
}
a) 1 2
b) 2 3
c) 3 2
d) 1 5
22. What is the output of this program?

class access
{
static int x;
void increment()
{
x++;
}
}
class static_use
{
public static void main(String args[])
{
access obj1 = new access();
access obj2 = new access();
obj1.x = 0;
obj1.increment();
obj2.increment();
System.out.println(obj1.x + " " + obj2.x);
}
}
a) 1 2
b) 1 1
c) 2 2
d) Compilation Error

23. What is the output of this program?

class static_out
{
static int x;
static int y;
void add(int a , int b)
{
x = a + b;
y = x + b;
}
}
class static_use
{
public static void main(String args[])
{
static_out obj1 = new static_out();
static_out obj2 = new static_out();
int a = 2;
obj1.add(a, a + 1);
obj2.add(5, a);
System.out.println(obj1.x + " " + obj2.y);
}
}
a) 7 7
b) 6 6
c) 7 9
d) 9 7
24. What is the output of this program?

class Output
{
public static void main(String args[])
{
int arr[] = {1, 2, 3, 4, 5};
for ( int i = 0; i < arr.length - 2; ++i)
System.out.println(arr[i] + " ");
}
}
a) 1 2
b) 1 2 3
c) 1 2 3 4
d) 1 2 3 4 5

25. What is the output of this program?

class output
{
public static void main(String args[])
{
String s1 = "Hello i love java";
String s2 = new String(s1);
System.out.println((s1 == s2) + " " + s1.equals(s2));
}
}
a) true true
b) false false
c) true false
d) false true

26. What will be the output of the program?

public class Foo


{
Foo()
{
System.out.print("foo");
}

class Bar
{
Bar()
{
System.out.print("bar");
}
public void go()
{
System.out.print("hi");
}
} /* class Bar ends */

public static void main (String [] args)


{
Foo f = new Foo();
f.makeBar();
}
void makeBar()
{
(new Bar() {}).go();
}
}/* class Foo ends */
A. Compilation fails.
B. An error occurs at runtime.
C. It prints "foobarhi"
D. It prints "barhi"

27. What will be the output of the program?

import java.util.*;
class I
{
public static void main (String[] args)
{
Object i = new ArrayList().iterator();
System.out.print((i instanceof List)+",");
System.out.print((i instanceof Iterator)+",");
System.out.print(i instanceof ListIterator);
}
}
A. Prints: false, false, false
B. Prints: false, false, true
C. Prints: false, true, false
D. Prints: false, true, true

28. What will be the output of the program?

class MyThread extends Thread


{
MyThread()
{
System.out.print(" MyThread");
}
public void run()
{
System.out.print(" bar");
}
public void run(String s)
{
System.out.println(" baz");
}
}
public class TestThreads
{
public static void main (String [] args)
{
Thread t = new MyThread()
{
public void run()
{
System.out.println(" foo");
}
};
t.start();
}
}
A. foo
B. MyThread foo
C. MyThread bar
D. foo bar

29. 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]);
}
}
}
and the command-line invocation is

> java CommandArgsThree 1 2 3

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

30. What will be the output of the program?

class MyThread extends Thread


{
MyThread() {}
MyThread(Runnable r) {super(r); }
public void run()
{
System.out.print("Inside Thread ");
}
}
class MyRunnable implements Runnable
{
public void run()
{
System.out.print(" Inside Runnable");
}
}
class Test
{
public static void main(String[] args)
{
new MyThread().start();
new MyThread(new MyRunnable()).start();
}
}
A. Prints "Inside Thread Inside Thread"
B. Prints "Inside Thread Inside Runnable"
C. Does not compile
D. Throws exception at runtime

31. What will be the output of the program?

public class CommandArgs


{
public static void main(String [] args)
{
String s1 = args[1];
String s2 = args[2];
String s3 = args[3];
String s4 = args[4];
System.out.print(" args[2] = " + s2);
}
}
and the command-line invocation is

> java CommandArgs 1 2 3 4

A. args[2] = 2
B. args[2] = 3
C. args[2] = null
D. An exception is thrown at runtime.

32. What will be the output of the program?

public class HorseTest


{
public static void main (String [] args)
{
class Horse
{
public String name; /* Line 7 */
public Horse(String s)
{
name = s;
}
} /* class Horse ends */

Object obj = new Horse("Zippo"); /* Line 13 */


Horse h = (Horse) obj; /* Line 14 */
System.out.println(h.name);
}
}

A. An exception occurs at runtime at line 10.


B. It prints "Zippo".
C. Compilation fails because of an error on line 7.
D. Compilation fails because of an error on line 13.
33. What will be the output of the program?

public class TestObj


{
public static void main (String [] args)
{
Object o = new Object() /* Line 5 */
{
public boolean equals(Object obj)
{
return true;
}
} /* Line 11 */

System.out.println(o.equals("Fred"));
}
}
A. It prints "true".
B. It prints "Fred".
C. An exception occurs at runtime.
D. Compilation fails

34. What will be the output of the program?

public abstract class AbstractTest


{
public int getNum()
{
return 45;
}
public abstract class Bar
{
public int getNum()
{
return 38;
}
}
public static void main (String [] args)
{
AbstractTest t = new AbstractTest()
{
public int getNum()
{
return 22;
}
};
AbstractTest.Bar f = t.new Bar()
{
public int getNum()
{
return 57;
}
};

System.out.println(f.getNum() + " " + t.getNum());


}
}
A. 57 22
B. 45 38
C. 45 57
D. An exception occurs at runtime.

35. What will be the output of the program?

public class Foo


{
Foo()
{
System.out.print("foo");
}

class Bar
{
Bar()
{
System.out.print("bar");
}
public void go()
{
System.out.print("hi");
}
} /* class Bar ends */

public static void main (String [] args)


{
Foo f = new Foo();
f.makeBar();
}
void makeBar()
{
(new Bar() {}).go();
}
}/* class Foo ends */

A. Compilation fails.
B. An error occurs at runtime.
C. It prints "foobarhi"
D. It prints "barhi"

36. public class MyOuter


{
public static class MyInner
{
public static void foo() { }
}
}

which statement, if placed in a class other than MyOuter or MyInner,


instantiates an instance of the nested class?
A. MyOuter.MyInner m = new MyOuter.MyInner();
B. MyOuter.MyInner mi = new MyInner();
C. MyOuter m = new MyOuter();
MyOuter.MyInner mi = m.new MyOuter.MyInner();
D. MyInner mi = new MyOuter.MyInner();
37. What is the output of this program?

class A
{
int i;
public void display()
{
System.out.println(i);
}
}
class B extends A
{
int j;
public void display()
{
System.out.println(j);
}
}
class Dynamic_dispatch
{
public static void main(String args[])
{
B obj2 = new B();
obj2.i = 1;
obj2.j = 2;
A r;
r = obj2;
r.display();
}
}

a) 1
b) 2
c) 3
d) 4

38. What will be the output of the program?


try
{
int x = 0;
int y = 5 / x;
}
catch (Exception e)
{
System.out.println("Exception");
}
catch (ArithmeticException ae)
{
System.out.println(" Arithmetic Exception");
}

System.out.println("finished");

A. finished
B. Exception
C. Compilation fails.
D. Arithmetic Exception
39. What will be the output of the program?

class Exc0 extends Exception { }


class Exc1 extends Exc0 { } /* Line 2 */
public class Test
{
public static void main(String args[])
{
try
{
throw new Exc1(); /* Line 9 */
}
catch (Exc0 e0) /* Line 11 */
{
System.out.println("Ex0 caught");
}
catch (Exception e)
{
System.out.println("exception caught");
}
}
}
A. Ex0 caught
B. exception caught
C. Compilation fails because of an error at line 2.
D. Compilation fails because of an error at line 9.

40. What will be the output of the program?

package foo;
import java.util.Vector; /* Line 2 */
private class MyVector extends Vector
{
int i = 1; /* Line 5 */
public MyVector()
{
i = 2;
}
}
public class MyNewVector extends MyVector
{
public MyNewVector ()
{
i = 4; /* Line 15 */
}
public static void main (String args [])
{
MyVector v = new MyNewVector(); /* Line 19 */
}
}
A. Compilation will succeed.
B. Compilation will fail at line 3.
C. Compilation will fail at line 5.
D. Compilation will fail at line 15.

You might also like