0% found this document useful (0 votes)
79 views15 pages

Java Programming Assessment Questions

The document contains 24 multiple choice questions about Java programming concepts. For each question there are 4 possible answers and the correct answer is provided. The questions cover topics like arrays, loops, operators, methods and more.

Uploaded by

mail.him.1994
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)
79 views15 pages

Java Programming Assessment Questions

The document contains 24 multiple choice questions about Java programming concepts. For each question there are 4 possible answers and the correct answer is provided. The questions cover topics like arrays, loops, operators, methods and more.

Uploaded by

mail.him.1994
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

Q1:- Which four options describe the correct default values for array elements of the

types indicated?
1. int -> 0
2. String -> "null"
3. Dog -> null
4. char -> '\u0000'
5. float -> 0.0f
6. boolean -> true

1,2,3,4

1,3,4,5

2,4,5,6

3,4,5,6
Correct Answer: - B
1,3,4,5

Q2:- Which one of these lists contains only Java programming language keywords?
class, if, void, long, Int, continue

goto, instanceof, native, finally, default, throws

try, virtual, throw, final, volatile, transient

strictfp, constant, super, implements, do


Correct Answer: - B
goto, instanceof, native, finally, default, throws

Q3:- Which will legally declare, construct, and initialize an array?


int [] myList = {"1", "2", "3"};

int [] myList = (5, 8, 2);

int myList [] [] = {4,9,7,0};

int myList [] = {4, 3, 7};


Correct Answer: - D
int myList [] = {4, 3, 7};

Q4:-
public void foo( boolean a, boolean b)
{
if( a )
{
[Link]("A"); /* Line 5 */
}
else if(a && b) /* Line 7 */
{
[Link]( "A && B");
}
else /* Line 11 */
{
if ( !b )
{
[Link]( "notB") ;
}
else
{
[Link]( "ELSE" ) ;
}
}
}

If a is true and b is true then the output is "A && B"

If a is true and b is false then the output is"notB"

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

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


Correct Answer: - C
If a is false and b is true then the output is"ELSE"

Q5:-
public void test(int x)
{
int odd = 1;
if(odd) /* Line 4 */
{
[Link]("odd");
}
else
{
[Link]("even");
}
}

Compilation Fails

"odd" will always be output.

"even" will always be the output.

"odd" will be output for odd values of x, and "even" for even values.
Correct Answer: - A
Compilation Fails

Q6:-
public class While
{
public void loop()
{
int x= 0;
while ( 1 ) /* Line 6 */
{
[Link]("x plus one is " + (x + 1)); /* Line 8 */
}
}
}
Which statement is true?

There is a syntax error on line 1.

There are syntax errors on lines 1 and 6.

There are syntax errors on lines 1, 6, and 8.

There is a syntax error on line 6.


Correct Answer: - D
There is a syntax error on line 6.

Q7:-
class PassA
{
public static void main(String [] args)
{
PassA p = new PassA();
[Link]();
}

void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
[Link](a1[0] + a1[1] + a1[2] + " ");
[Link](a2[0] + a2[1] + a2[2]);
}

long [] fix(long [] a3)


{
a3[1] = 7;
return a3;
}
}

12 15

15 15

345375

375375
Correct Answer: - B
15 15

Q8:-
class Test
{
public static void main(String [] args)
{
Test p = new Test();
[Link]();
}

void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
[Link](b1 + " " + b2);
}

boolean fix(boolean b1)


{
b1 = true;
return b1;
}
}
true true

false true

true false

false false
Correct Answer: - B
false true

Q9:-
class PassS
{
public static void main(String [] args)
{
PassS p = new PassS();
[Link]();
}

void start()
{
String s1 = "slip";
String s2 = fix(s1);
[Link](s1 + " " + s2);
}

String fix(String s1)


{
s1 = s1 + "stream";
[Link](s1 + " ");
return "stream";
}
}

slipstream

slip stream

slipstream slip

slipstream slip stream


Correct Answer: - D
slipstream slip stream

Q10:-
class Equals
{
public static void main(String [] args)
{
int x = 100;
double y = 100.1;
boolean b = (x = y); /* Line 7 */
[Link](b);
}
}
What will be the output of the program?

TRUE

FALSE

Compilation Fails

Exception at Runtime
Correct Answer: - C
Compilation Fails

Q11:-
class Test
{
public static void main(String [] args)
{
int x=20;
String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";
[Link](sup);
}
}
What will be the output of the program?

small

tiny

huge

Compilation Fails
Correct Answer: - B
tiny

Q12:-
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++;
}
}
[Link](x + " " + y);
}
}

52

53
63

64
Correct Answer: - C
63

Q13:-
class Bitwise
{
public static void main(String [] args)
{
int x = 11 & 9;
int y = x ^ 3;
[Link]( y | 12 );
}
}

14
Correct Answer: - D
14

Q14:-
class SSBool
{
public static void main(String [] args)
{
boolean b1 = true;
boolean b2 = false;
boolean b3 = true;
if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */
[Link]("ok ");
if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/
[Link]("dokey");
}
}

ok

dokey

ok dokey

Compilation Fails
Correct Answer: - B
dokey

Q15:-Which of the following are legal lines of code?


1. int w = (int)888.8;
2. byte x = (byte)1000L;
3. long y = (byte)100;
4. byte z = (byte)100L;

1 and 2

2 and 3

3 and 4

All Statements are correct.


Correct Answer: - D
All Statements are correct.

Q16:-
class CompareReference
{
public static void main(String [] args)
{
float f = 42.0f;
float [] f1 = new float[2];
float [] f2 = new float[2];
float [] f3 = f1;
long x = 42;
f1[0] = 42.0f;
}
}
import [Link];
class CompareReference
{
public static void main(String [] args)
{
float f = 42.0f;
float [] f1 = new float[2];
float [] f2 = new float[2];
float [] f3 = f1;
long x = 42;
f1[0] = 42.0f;
}
}
which three statements are true?
1. f1 == f2
2. f1 == f3
3. f2 == f1[1]
4. x == f1[0]
5. f == f1[0]

1,2 and 3

2, 4 and 5

3, 4 and 5

1, 4 and 5
Correct Answer: - B
2, 4 and 5

Q17:- Which three forms are a part of correct array declarations?


1. public int a [ ]
2. static int [ ] a
3. public [ ] int a
4. private int a [3]
5. private int [3] a [ ]
6. public final int [ ] a

1, 3 and 4

2, 4 and 5
1, 2 and 6

2, 5 and 6
Correct Answer: - C
1, 2 and 6

Q18:- Which cause a compiler error?


A. int[ ] scores = {3, 5, 7};

B. int [ ][ ] scores = {2,7,6}, {9,3,45};

C. String cats[ ] = {"Fluffy", "Spot", "Zeus"};

D. boolean results[ ] = new boolean [] {true, false, true};


Correct Answer: - B
B. int [ ][ ] scores = {2,7,6}, {9,3,45};

Q19:-
class SC2
{
public static void main(String [] args)
{
SC2 s = new SC2();
[Link]();
}

void start()
{
int a = 3;
int b = 4;
[Link](" " + 7 + 2 + " ");
[Link](a + b);
[Link](" " + a + b + " ");
[Link](foo() + a + b + " ");
[Link](a + b + foo());
}

String foo()
{
return "foo";
}
}

9 7 7 foo 7 7foo

72 34 34 foo34 34foo

9 7 7 foo34 34foo

72 7 34 foo34 7foo
Correct Answer: - D
72 7 34 foo34 7foo

Q20:-
class Test
{
static int s;
public static void main(String [] args)
{
Test p = new Test();
[Link]();
[Link](s);
}

void start()
{
int x = 7;
twice(x);
[Link](x + " ");
}

void twice(int x)
{
x = x*2;
s = x;
}
}

77

7 14
14 0

14 14
Correct Answer: - B
7 14

Q21:-
class BoolArray
{
boolean [] b = new boolean[3];
int count = 0;

void set(boolean [] x, int i)


{
x[i] = true;
++count;
}

public static void main(String [] args)


{
BoolArray ba = new BoolArray();
[Link](ba.b, 0);
[Link](ba.b, 2);
[Link]();
}

void test()
{
if ( b[0] && b[1] | b[2] )
count++;
if ( b[1] && b[(++count - 2)] )
count += 7;
[Link]("count = " + count);
}
}

count = 0

count = 2

count = 3
count = 4
Correct Answer: - C
count = 3

Q22:-
public class Test
{
public static void leftshift(int i, int j)
{
i <<= j;
}
public static void main(String args[])
{
int i = 4, j = 2;
leftshift(i, j);
[Link](i);
}
}

16
Correct Answer: - B
4

Q23:-
class A{
int a = 100;
void m1(){
int a;
[Link](a);
}
public static void main(String arg[]){
new A().m1();
}
}
10

Compilation Fails

Compilation Success but no error


Correct Answer: - C
Compilation Fails

Q24:-
class A{
public static void main(String arg[]){
int ar1[] = null;
ar1 = new int[5];
[Link](ar1[0]);
ar1 = new int[]{1,2,3,4,5};
[Link](ar1[0]);
}
}

01

00

Compilation Fails

Exception at Runtime
Correct Answer: - A
01

Common questions

Powered by AI

The method 'twice' modifies its parameter 'x' by doubling it and assigns this value to the static variable 's'. Since 's' is a class-level variable, the change persists outside the method scope. The initially declared local 'x' doesn't affect 's' directly, but 'twice(x)' computes 'x * 2', setting 's' to '14', which remains when printed .

In Java, arguments to functions are passed by value, meaning that for primitive types like boolean, the function receives a copy of the original argument. In the provided code, 'b1' in the 'fix' method is a local copy; setting it to true does not alter the original 'b1' in 'start' method. Therefore, 'false' (the original value) is printed first, followed by 'true' (returned by the 'fix' method).

In this Java program, the 'fix' method concatenates "stream" to 's1', a local copy of the original string reference. This modified string is printed inside 'fix', but the original string 's1' in 'start' remains "slip" because strings in Java are immutable and passing by value means the original reference isn't altered. Hence, "slip stream" is printed .

The code performs several bitwise operations as follows: '11 & 9' computes to '9' because in binary it results in 1001. 'y = x ^ 3' calculates to '10' because 1001 XOR 0011 equals 1010. Finally, 'y | 12' gives '14' as 1010 OR 1100 results in 1110, outputting the decimal value '14' .

The code uses a nested ternary operator to determine the string value assigned to 'sup'. It first checks if 'x' is less than 15; if true, returns "small". If false, it checks if 'x' is less than 22; if true, returns "tiny". If both conditions are false, it defaults to "huge". As 'x' equals 20, the first condition is false and the second is true, thus 'sup' is set to "tiny" .

In Java, an incorrect multi-dimensional array declaration like 'int [ ][ ] scores = {2,7,6}, {9,3,45};' generates a compile-time error because each sub-array is not enclosed in its own braces. Proper syntax requires curling each sub-array in braces: 'int [ ][ ] scores = {{2,7,6},{9,3,45}};', allowing the array to be initialized with sub-arrays correctly .

The statements involve explicit casting, which Java handles without runtime errors though these conversions might truncate values. 'byte x = (byte)1000L;' wraps around as bytes range from -128 to 127, so 1000 translates into -24 (1000 mod 256). Also, '{int w = (int)888.8;}' cleanly truncates the float to 888. Explicit casts ensure legal, yet possibly data-altered conversion at compile-time, free from exceptions .

Java permits explicit casting between different primitive types, even if this could potentially result in loss of information. In the given statements, '(int)888.8' truncates the floating point to an integer value, '(byte)1000L' and '(byte)100L' both narrow a long integer to the byte range, wrapping the values to fit within byte's capacity, and '(byte)100' simply converts an integer to a byte. All lines are legal due to explicit casts handling potential information loss .

The program has a syntax error on line 6, where 'while ( 1 )' is used. In Java, the while condition expects a boolean expression, but '1' is an integer, not automatically convertible to a boolean. Therefore, the compiler will raise a syntax error at this line .

The program will output "notB". When the method 'foo' is called with 'a' as true and 'b' as false, the first if condition 'if( a )' is satisfied. Thus, "A" is printed, ignoring all other conditions. However, based on the corrected understanding, if we reconsider solely the conditions: if 'a' is false and 'b' is false, the output is "ELSE", which aligns with the logic where else block executes when all prior conditions fail .

You might also like