Core Java Basic Assesment-1 Solution

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

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 )
{
System.out.println("A"); /* Line 5 */
}
else if(a && b) /* Line 7 */
{
System.out.println( "A && B");
}
else /* Line 11 */
{
if ( !b )
{
System.out.println( "notB") ;
}
else
{
System.out.println( "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 */
{
System.out.println("odd");
}
else
{
System.out.println("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 */
{
System.out.print("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();
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;
}
}

12 15

15 15

345375

375375
Correct Answer: - B
15 15

Q8:-
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;
}
}
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();
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";
}
}

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 */
System.out.println(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";
System.out.println(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++;
}
}
System.out.println(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;
System.out.println( 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 */
System.out.print("ok ");
if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/
System.out.println("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 java.awt.Button;
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();
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";
}
}

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();
p.start();
System.out.println(s);
}

void start()
{
int x = 7;
twice(x);
System.out.print(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();
ba.set(ba.b, 0);
ba.set(ba.b, 2);
ba.test();
}

void test()
{
if ( b[0] && b[1] | b[2] )
count++;
if ( b[1] && b[(++count - 2)] )
count += 7;
System.out.println("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);
System.out.println(i);
}
}

16
Correct Answer: - B
4

Q23:-
class A{
int a = 100;
void m1(){
int a;
System.out.println(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];
System.out.println(ar1[0]);
ar1 = new int[]{1,2,3,4,5};
System.out.println(ar1[0]);
}
}

01

00

Compilation Fails

Exception at Runtime
Correct Answer: - A
01

You might also like