19
Discussion1 Quiz

Discussion1 Quiz. Q1 Which of the following are invalid Java identifiers (variable names)? a) if b) n4m3 c) Java d) 12345 e) DEFAULT_VALUE f) bad-choice

Embed Size (px)

Citation preview

Discussion1 Quiz

Q1

Which of the following are invalid Java identifiers (variable names)?

a) ifb) n4m3c) Javad) 12345e) DEFAULT_VALUEf) bad-choiceg) 1two3h) first namei) last.name

A1

Which of the following are invalid Java identifiers (variable names)?

a) ifb) n4m3c) Javad) 12345e) DEFAULT_VALUEf) bad-choiceg) 1two3h) first namei) last.name

Q2

Which of these identifiers violate the naming convention for object names?

 

a) R2D2

b) isthisokay?

c) Java

d) 3CPO

e) ThisIsReallyOkay

f) Java

g) anotherbadone

A2

Which of these identifiers violate the naming convention for object names?

 

a) R2D2

b) isthisokay?

c) Java

d) 3CPO

e) ThisIsReallyOkay

f) Java

g) anotherbadone

Q3

What is the output of the following statements

a) System.out.println( (a++)++ );

b) System.out.println( 2.5 == 5 / 2 );

c) System.out.println( 2.0 + 3/2 );

d) System.out.println( 5 % 3);

e) System.out.println(5 + 4/2.0);

f) float f = 1.1;System.out.println(f – 0.1);

g) int a = 10; System.out.println(a = 11);

A3

What is the output of the following statements

a) System.out.println( (a++)++ ); //error!

b) System.out.println( 2.5 == 5 / 2 ); //false

c) System.out.println( 2.0 + 3/2 ); //3.0

d) System.out.println( 5 % 3); //2

e) System.out.println(5 + 4/2.0); // 7.0

f) float f = 1.1;System.out.println(f – 0.1); //error!

g) int a = 10; System.out.println(a = 11); //11

Q4

What is the result?boolean b1 = true;Boolean b2 = false;System.out.println(b1 || b2 = true);System.out.println(b1);System.out.println(b2);

A4

What is the result?boolean b1 = true;Boolean b2 = false;System.out.println(b1 || b2 = true);//b1 || b2 is evaluated first before =System.out.println(b1);System.out.println(b2);

Q5

What is the result?boolean b1 = true;Boolean b2 = false;System.out.println(b1 || (b2 = true));System.out.println(b1);System.out.println(b2);

A5

What is the result?boolean b1 = true;Boolean b2 = false;System.out.println(b1 || (b2 = true));

//trueSystem.out.println(b1); //trueSystem.out.println(b2); //false

Q6

What is the result?boolean b1 = true;Boolean b2 = false;System.out.println(b1 && (b2 = true));System.out.println(b1);System.out.println(b2);

A6

What is the result?boolean b1 = true;Boolean b2 = false;System.out.println(b1 && (b2 = true)); //trueSystem.out.println(b1); //trueSystem.out.println(b2); //true

Q7

What’s the output?String s1 = new String("John");

String s2 = new String("John");

if (s1 == s2)

System.out.println("s1 == s2 is true");

else

System.out.println("s1 == s2 is false");

A7

What’s the output?String s1 = new String("John");

String s2 = new String("John");

if (s1 == s2)

System.out.println("s1 == s2 is true");

else

System.out.println("s1 == s2 is false");

// s1 == s2 is false

Q8

Is there any problem with this?int a = 10;double b = 1.0;a = a + b;System.out.println(a);

A8

Is there any problem with this?int a = 10;double b = 1.0;a = a + b; //type errorSystem.out.println(a);

Q8

Is there any problem with this?int a = 10;double b = 1.0;a += b;System.out.println(a);

A8

Is there any problem with this?int a = 10;double b = 1.0;a += b;System.out.println(a);

//no problem!lhs op= rhsis actuallylhs op= (type of lhs)rhs