Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

TTWRDC,SpARK, HYDERBAD

WORKSHEET-10
CORE CONCEPTS IN JAVA

1.Predict the output of the following code modules

class Main { ( )
public static void main(String args[]) {
int t;
System.out.println(t);
}
}
a. 0 b. garbage value c. compile error d. Runtime
error

2. class Test { ( )

public static void main(String[] args) {

for(int i = 0; 0; i++)

System.out.println("Hello");

break;

} }}

a. Hello b. empty output c. compile error d. runtime


error

3. Which of the following statements is/are TRUE regarding JAVA ? ( )

(1) Constants that cannot be changed are declared using the ‘static’ keyword.

(2) A class can only inherit one class but can implement multiple interfaces.

a. 1 only b. 2 only c. both d. none of these

4. Output of following Java program? ( )

class Main {
public static void main(String args[]) {
System.out.println(fun());
}

int fun()
{
return 20;
}
}
a. Compile error b.20 c. 0 d. garbage value
5. Output of the following program ( )

public class Main {

public static void main(String args[]) {


String x = null;
giveMeAString(x);
System.out.println(x);
}
static void giveMeAString(String y)
{
y = "CUCET";
}
}

6. class Test { ( )

public static void swap(Integer i, Integer j) {


Integer temp = new Integer(i);
i = j;
j = temp;
}
public static void main(String[] args) {
Integer i = new Integer(10);
Integer j = new Integer(20);
swap(i, j);
System.out.println("i = " + i + ", j = " + j);
}
}
a. i=10,j=20 b. i=20, j=10 c. i=10, j=10 d. i=20,j=20

7. Predict the output? ( )

class Main {
public static void main(String args[]) {
System.out.println(fun());
}
static int fun(int x = 0)
{
return x;
}
}
a. 0 b. garbage value c. compile error d. runtime error

8. Predict the output ( )


1. public class While
2 {
3 public void loop()
4 {
5 int x = 0;
6 while(1)
7 {
8 System.out.println("x plus one is" +(x+1));
9 }
10 }
11 }
a. There is a syntax error in 1. b. There is a syntax error in 1and 6
c. There is a syntax error in 8 d. There is a syntax error in 6
9. //precondition: x>=0
public void demo(int x)
{
System.out.print(x % 10);
if (x % 10 != 0)
{
demo(x/10);
}
System.out.print(x%10);
}

Which of the following is printed as a result of the call demo(1234)? ( )


a. 1441 b. 3443 c. 12344321 d.43211234

10. Predict the output of following Java Program ( )

class Test {
public static void main(String args[]) {
int x = -4;
System.out.print(x>>1+ “ “);
int y = 4;
System.out.println(y>>1);
}
}

a. Compiler Error: Operator >> cannot be applied to negative numbers


b. -2 2 c.2 2 d.0 2

11. Predict the output of following Java program. Assume that int is stored using 32 bits.( )

class Test {
public static void main(String args[]) {
int x = -1;
System.out.println(x>>>29+” “);
System.out.println(x>>>30+” “);
System.out.println(x>>>31);
}
}

a. 15 7 3 b. 0 0 0 c.1 1 1 d. 7 3 1

12. class Test { ( )

public static void main(String args[]) {


System.out.println(10 + 20 + "CUCET" + “ “);
System.out.println("CUCET" + 10 + 20);
}
}
a. 1020CUCET CUCET1020 b.30CUCET CUCET30
c.30CUCET CUCET1020 d. 1020CUCET CUCET30

13. . class Test { ( )

public static void main(String args[]) {


System.out.println(10 * 20 + "CUCET" + “ “);
System.out.println("CUCET" + 10 *20);
}
}
a. 1020CUCET CUCET200 b.200CUCET CUCET200
c.200CUCET CUCET1020 d. 1020CUCET CUCET200

14. Which of the following is not an operator in Java? ( )

a. instaceof b.sizeof c.new d.>>>=

15. Predict the output of the following ( )

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. 8 2 b. 8 3 c.8 5 d.5 3

16. In Java, after executing the following code what are the values of x, y and z? int x,y=10;
z=12; x=y++ + z++; ( )

a. x=22, y=10, z=12 b. x=24, y=10, z=12

c. x=24, y=11, z=13 d. x=22, y=11, z=13

17. Consider the following code segment


for (int k=0; k<20; k=k+2)
{
if (k % 3 == 1)
system.out.print(k+ " ")
}
What is printed as a result of executing the code segment? ( )

a. 4 16 b. 4 10 16 c. 0 6 12 18 d. 1 4 7 10 13 16 19

18. 1. Which of these selection statements test only for equality? ( )


a. if b. switch c. if & switch d. none of the mentioned

19. Which of the following loops will execute the body of loop even when condition controlling
the loop is initially fal ( )
a. do-while b).while c. for d. none of the mentioned

20. What will be the output of the following Java program? ( )

1. class selection_statements
2. {
3. public static void main(String args[])
4. {
5. int var1 = 5;
6. int var2 = 6;
7. if ((var2 = 1) == var1)
8. System.out.print(var2);
9. else
10. System.out.print(++var2);
11. }
12. }
a.1 b.2 c.3 d.4

You might also like