wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Logic Building in Java Part 1

Total questions: 10

Worksheet time: 5mins

Name
Class
Date
1.

What is the output of the following code?

if (a){

System.out.println("A");

}

else if(a && b){

System.out.println("A && B");

}

else {

if(!b) {

System.out.println("notB");

}

else{

System.out.println("ELSE");

}

}

a)

If a is true and b is true then output is A && B.

b)

If a is true and b is false then output is notB.

c)

If a is false and b is true then the output is ELSE.

d)

If a is false and b is false then the output is ELSE.

2.

What is the value of discount after the code block?

char gender='F';

byte discount;

if (gender='M') {

discount=10;

}

else{

discount=20;

}

a)

10

b)

Compilation Error

c)

20

d)

Runtime Error

3.

What will be the output of the following program?

public class ShortTest {

public static void main(String[] args) {

boolean x = true;

boolean y = false;

if (x || y) {

System.out.println(true);

}

else {

System.out.println(false);

}

}

}

a)

False

b)

Runtime Error

c)

True

d)

Compilation Error

4.

Given the following:

long[][] stuff ;

Which of the following statements constructs an array with 5 rows of 7 columns each and assign its reference to stuff ?

a)

stuff = new stuff[5][7] ;

b)

stuff = long[5][7] ;

c)

stuff = long[7][5] ;

d)

stuff = new long[5][7] ;

5.

Examine the following:

double[][] values =

{ {1.2, 9.0, 3.2},

{9.2, 0.5, 1.5, -1.2},

{7.3, 7.9, 4.8} } ;

What is in values[3][0] ?

a)

7.3

b)

Error

c)

7.9

d)

9.2

6.

From the following select the right way to invoke following method in the main method

class Example

{

static int add(inta,int b) {

return a+b;

}

}

a)

add(20,30)

b)

Example obj=new Example(); obj.add(10,20);

c)

Static.add(10,20);

d)

Example.add(10,20)

7.

What is the output of the following code?

class Main{

public static void main(String []s)

{

float x=12.34F;

float y=13.25F;

float result=min(x,y);

System.out.println(result);

}

public static float min(float a,float b)

{

float minimun=a>b?b:a;

}

}

a)

Compilation Error- function should have a return statement

b)

12.34

c)

Compilation Error - cannot invoke method without creating object

d)

Runtime Error

8.

What will be output of following code?

import java.util.Scanner;

public class Main

{

public static Integer add(Integer a, Integer b) {

return a + b;

}

public static void main(String args[])

{

System.out.println(add(5,6));

}

}

a)

11

b)

Compilation Error

c)

Runtime Error

d)

Compile and execute without any Exception or error

9.

Choose the option to fill the blanks.

public class Power {

public static void main(String[] args) {

int base = 3, exponent = 4;

long result=_____;

System.out.println("3 power 4 = " + result);

}

public static long power(int b,int e) {

long result=1;

for (;e != 0; --e) {

result * = b;

}

return result;

}

}

a)

power(b,e);

b)

power(base,exponent);

c)

3 power 4 = 81

d)

power(e,base);

10.

Choose a code snippet from the following to perform power(x,n) an the output is Answer = 8

a)

while (n<=0)

{

r += x;

n++;

}

b)

while (n >= 0)

{

r *= x;

n++;

}

c)

while (n > 0)

{

r += x;

--n;

}

d)

while (n != 0)

{

r *= x;

--n;

}