Java Operators and statements M03

¡Supera tus tareas y exámenes ahora con Quizwiz!

It will print 3, 2 when line 1 is replaced by break; It will print 4, 3 when line 1 is replaced by continue. t will print 3, 3 when line 1 is replaced by i = 4;

Given the following code, which of these statements are true?Each correct answer represents a complete solution. Choose all that apply.class TestClass{public static void main(String args[]){int k = 0;int m = 0;for ( int i = 0; i <= 3; i++){ k++; if ( i == 2){ // line 1 } m++;}System.out.println( k + ", " + m );}}

It will print numbers from 0 to 4

Question 19 :What will the following code print?void crazyLoop(){int c = 0;JACK: while (c < 8){JILL: System.out.println(c);if (c > 3) break JACK; else c++;}}

One

The minimum number of times the do-while loop is executed in a program is _____.

False

Using a break in a while loop causes the loop to break the current iteration and start the next iteration of the loop.

It will print 3

What will be the result of attempting to compile and run the following program? class TestClass{public static void main(String args[]){boolean b = false;int i = 1;do{ i++ ;} while (b = !b);System.out.println( i );}}

do { break ; } while (true) ; switch (1) { default : break; } for ( ; true ; ) break ;

Which of these statements are valid when occurring by themselves in a method?

Enhanced for loop can by used for only the first task. For the rest, standard for loops can be used.

You have been given an array of objects and you need to process this array as follows -Call a method on each object from first to last one by one.Call a method on each object from last to first one by one.Call a method on only those objects at even index (0, 2, 4, 6, etc.)Which of the following are correct?

19

Consider the following expression:a=20-5%2What will be the value of the a variable?

24

Consider the following method: static int mx(int s) {for (int i = 0; i < 3; i++) {s = s + i;}return s;} and the following code snippet: int s = 5;s += s + mx(s) + ++s;System.out.println(s); What will it print?

It will print 1 2 3 4 5 six times

Given: package loops;public class JustLooping {private int j;void showJ() {while (j <= 5) { for (int j = 1; j <= 5;) { System.out.print(j + " "); j++; } j++;}}public static void main(String[] args) {new JustLooping().showJ();}} What is the result?

Caught AE 2 10 5 2

Given: public class CrazyMath { public static void main(String[] args) { int x = 10, y = 20; int dx, dy; try{ dx = x % 5; dy = y/dx; }catch(ArithmeticException ae){ System.out.println("Caught AE"); dx = 2; dy = y/dx; } x = x/dx; y = y/dy; System.out.println(dx+" "+dy); System.out.println(x+" "+y); }} What is the output?

6 4

Given: public class SimpleLoop {public static void main(String[] args) {int i = 0, j = 10;while (i <= j) { i++; j--;}System.out.println(i + " " + j);}} What is the result?

36

What is the result of the following code snippet? 3: int m = 9, n = 1, x = 0;4: while(m > n) {5: m--;6: n += 2;7: x += m + n;8: }9: System.out.println(x);

3

What will the following code print? boolean flag = true;if(flag = false){System.out.println("1");}else if(flag){System.out.println("2");}else if(!flag){System.out.println("3");}else System.out.println("4");

long

Which of the following data types is not supported by the switch statement?

do { //statements } while (boolean_expression);

Which of the following is the correct syntax of the do-while loop?

!

Which of the following operators has the highest order of precedence?

As such, the class will compile and print "20 " (without quotes) at the end of its output. It will not compile if line 3 is removed. It will not compile if line 3 is removed and placed before line 1. It will not compile if line 4 is removed and placed before line 3.

Consider the following class : class Test{public static void main(String[] args){for (int i = 0; i < 10; i++) System.out.print(i + " "); //1for (int i = 10; i > 0; i--) System.out.print(i + " "); //2int i = 20; //3System.out.print(i + " "); //4}} Which of the following statements are true? Each correct answer represents a complete solution. Choose all that apply.

i=5 j=6

Consider the following code for the main() method: public static void main(String[] args) throws Exception{int i = 1, j = 10;do {if (i++ > --j) continue;} while (i < 5);System.out.println("i=" + i + " j=" + j);} What will be the output when the above code is executed?

Integer condition = new Integer("1"); Byte condition = 1;

Consider the following code snippet: //INSERT LINE OF CODE HEREswitch( condition ){case 1: System.out.println("1");break;case 2: System.out.println("2");break;case 3 : System.out.println("3");break;} What type can be inserted in the code above so that the above code compiles and runs as expected? Each correct answer represents a complete solution. Choose two.

3

Consider the following code: String[] dataList = { "x", "y", "z"};for (String dataElement: dataList) {int innerCounter = 0;while (innerCounter < dataList.length) {System.out.println(dataElement + ", " + innerCounter);innerCounter++;}} How many times will the output contain 2?

It will not print 0

Consider the following code: public class Conversion{ public static void main(String[] args){ int i = 1234567890; float f = i; System.out.println(i - (int)f); }} What will it print when run?

3

Consider the following code:public static void main(String[] args) {int[] values = { 10, 30, 50 };for( int val : values ){ int x = 0; while(x<values.length){ System.out.println(x+" "+val); x++; }} }' How many times is 2 printed out in the output

It will not compile

Consider the following method which is called with an argument of 7: public void method1(int i) {int j = (i * 30 - 2) /100;POINT1 : for(;j<10; j++){boolean flag = false;while (!flag) {if (Math.random() > 0.5) break POINT1;}}while (j > 0) {System.out.println(j--);if (j == 4) break POINT1;}} What will it print?(Assume that Math.random() return a double between 0.0 and 1.0, not including 1.0)

It will compile and print def It will compile for any int values in calculate(...);

Consider the following program: public class TestClass {public static void main(String[] args) {calculate(2);}public static void calculate(int x) {String val;switch (x) { case 2: default: val = "def";}System.out.println(val);}} What will happen if you try to compile and run the program? Each correct answer represents a complete solution. Choose all that apply.

o1 == o2 will always be false. Nothing can be said about o1.equals(o2) regarding what it will return based on the given information.

Consider:o1 and o2 denote two object references to two different objects of the same class.Which of the following statements are true? Each correct answer represents a complete solution. Choose all that apply.

Hello World

Given: public class TestClass{public static int getSwitch(String str){return (int) Math.round( Double.parseDouble(str.substring(1, str.length()-1)) );}public static void main(String args []){switch(getSwitch(args[0])){case 0 : System.out.print("Hello ");case 1 : System.out.print("World"); break;default : System.out.print("Good Bye");}}} What will be printed by the above code if it is run with command line:java TestClass --0.50(There are two minuses before 0.)

for(;Math.random()<0.5;){System.out.println("true");} for(;;Math.random()){System.out.println("true");}

Identify valid for constructs...Assume that Math.random() returns a double between 0.0 and 1.0 (not including 1.0).Each correct answer represents a complete solution. Choose all that apply.

It will print 1 2 3 4

In the following code what will be the output if 0 (integer value zero) is passed to loopTest()?public class TestClass{public void loopTest(int x){loop: for (int i = 1; i < 5; i++){ for (int j = 1; j < 5; j++){ System.out.println(i); if (x == 0) { continue loop; } System.out.println(j); }}}}

1 2 4 5

Question 20 :What will the following code print when compiled and run? int[][] ab = {{1, 2, 3}, {4, 5}};for (int i = 0; i < ab.length; i++){ for (int j = 0; j < ab[i].length; j++){ System.out.print(ab[i][j] + " "); if (ab[i][j] == 2) { break; }}continue;}

25

Question 27 :Given: int a = 1 + 2 + 3 * 4;int b = 2 * 3 + 4;int total = a + b; What will be the value of total?

continue; and //nothing is required continue; and scontinue;

What can be inserted in the following code so that it will print exactly 2345 when compiled and run? Each correct answer represents a complete solution. Choose two. public class FlowTest {static int[] data = {1, 2, 3, 4, 5};public static void main(String[] args) {for (int i: data) { if (i < 2) { //insert code1 here } System.out.print(i); if (i == 3) { // insert code2 here }}}}

Cast x on line 4 to int. Change the data type of x on line 3 to short. Cast 2 * x on line 4 to int Change the data type of y on line 4 to long

What change would allow the following code snippet to compile? (Choose all that apply.) 3: long x = 10;4: int y = 2 * x;

for(initialization; booleanExpression; updateStatement)

What is the correct order of the for loop statement?

The code will not compile because of line 5

What is the output of the following application? public class CompareValues { public static void main(String[] args) { int x = 0; while (x++ < 10) {} String message = x > 10 ? "Greater than" : false; System.out.println(message + "," + x); } }

20

What is the output of the following code snippet? class increment { public static void main(String args[]) { int x = 4; System.out.print(x++ * 5); }

true, 20, false

What is the output of the following code snippet? 3: boolean x = true, z = true;4: int y = 20;5: x = (y != 10) ^ (z=false);6: System.out.println(x+", "+y+", "+z);

11

What is the output of the following code snippet?3: boolean keepGoing = true;4: int result = 15, i = 10;5: do {6: i--;7: if (i == 8) keepGoing = false;8: result -= 2;9:} while (keepGoing);10: System.out.println(result);

The code will not compile because of line 5

What is the output of the following code snippet?3: int x = 0;4: String s = null;5: if(x == s) System.out.println("Success");6: else System.out.println("Failure");

The code will not compile because of line 4.

What is the output of the following code? 3: byte a = 40, b = 50;4: byte sum = (byte) a + b;5: System.out.println(sum);

2

What is the output of the following code? public class ArithmeticSample { public static void main(String[] args) { int x = 5 * 4 % 3; System.out.println(x); } }

8

What is the output of the following code? public class TernaryTester { public static void main(String[] args) { int x = 5; System.out.println(x > 2 ? x < 4 ? 10 : 8 : 7); } }

Use of two different data types

What is the reason behind the unsuccessful compilation of the following code? for(long y = 0, int x = 4; x < 5 && y<10; x++, y++) { System.out.print(x + " "); }

It will print 1 0 2

What is the result of executing the following code when the value of i is 5: switch (i){default:case 1:System.out.println(1);case 0:System.out.println(0);case 2:System.out.println(2);break;case 3:System.out.println(3);}

Compile time error

What is the result of executing the following fragment of code: boolean b1 = false;boolean b2 = false;if (b2 != b1 = !b2){System.out.println("true");}else{System.out.println("false");}

It will print true

What is the result of executing the following fragment of code: boolean b1 = false;boolean b2= false;if (b2 = b1 == false){System.out.println("true");} else{System.out.println("false");}

greatgood

What is the result of the following code snippet? 3: final char a = 'A', d = 'D';4: char grade = 'B';5: switch(grade) {6: case a:7: case 'B': System.out.print("great");8: case 'C': System.out.print("good"); break;9: case d:10: case 'F': System.out.print("not good");11: }

None of these.

What will be printed by the following code if it is run with command line: java TestClass -0.50? public class TestClass {public static double getSwitch(String str) {return Double.parseDouble(str.substring(1, str.length() - 1));}public static void main(String args[]) {switch (getSwitch(args[0])) { case 0.0: System.out.println( " Hello "); case 1.0: System.out.println( " World "); break; default: System.out.println( " Good Bye ");}}}

It will print 2

What will be the output of the following program? public class TestClass{public static void main(String args[ ] ){int i = 0 ;boolean bool1 = true ;boolean bool2 = false;boolean bool= false;bool = ( bool2 &method1(i++) ); //1bool = ( bool2 && method1(i++) ); //2bool = ( bool1 |method1(i++) ); //3bool = ( bool1 || method1(i++) ); //4System.out.println(i);}public static boolean method1(int i){return i>0? true : false;}}

0 0 will be printed once.

What will be the output when the following program is run? public class TestClass{public static void main(String args[]){int i;int j;for (i = 0, j = 0; j < i; ++j, i++){ System.out.println(i + " " + j);}System.out.println(i + " " + j);}}

The program will write 0 to the standard output.

What will be the result of attempting to compile and run the following program? public class TestClass{public static void main(String args[]){int x = 0;labelA: for (int i=10; i<0; i--){ int j = 0; labelB: while (j < 10){ if (j > i) break labelB; if (i == j){ x++; continue labelA; } j++; } x--;}System.out.println(x);}}

The code will compile without error and will terminate without problems when run.

What will be the result of attempting to compile and run the following program? class TestClass{public static void main(String args[]){int i = 0;for (i=1 ; i<5 ; i++) continue; //(1)for (i=0 ; ; i++) break; //(2)for ( ; i<5?false:true ; ); //(3)}}

It will print 30 20 30

What will be the result of trying to compile and execute of the following program? public class TestClass{ public static void main(String args[] ){ int i = 0 ; int[] iA = {10, 20} ; iA[i] = i = 30 ; System.out.println(""+ iA[ 0 ] + " " + iA[ 1 ] + " "+i) ; }}

It will print 14, 25

What will the following class print? class InitTest{ public static void main(String[] args){ int a = 10; int b = 20; a += (a = 4); b = b + (b = 5); System.out.println(a+ ", "+b); }}

It will not compile.

What will the following code print when compiled and run: public class TestClass {public static void main(String[] args) {int k = 2;while (--k) { System.out.println(k);}}}

It will not compile

What will the following code print when compiled and run: public class TestClass {public static void main(String[] args) {while (int k = 5; k < 7) { System.out.println(k++);}}}

It will not compile

What will the following code print when compiled and run?public class TestClass{public static void main(String[] args){int[] arr = { 1, 2, 3, 4, 5, 6 };int counter = 0;for (int value : arr) {if (counter >= 5) {break;} else {continue;}if (value > 4) {arr[counter] = value + 1;}counter++;}System.out.println(arr[counter]);}}

It will not compile

What will the following code print when run?public class TestClass {public static void main(String[] args) throws Exception {boolean flag = true;switch (flag) { case true: System.out.println( " true "); default: System.out.println( " false ");}}}

25 , 25

What will the following code print? class Test{public static void main(String[] args){int k = 1;int[] a = { 1 };k += (k = 4) * (k + 2);a[0] += (a[0] = 4) * (a[0] + 2);System.out.println( k + " , " + a[0]);}}

1 2

What will the following code print? int i = 0;int j = 1;if( (i++ == 0) & (j++ == 2) ){i = 12;}System.out.println(i+" "+j);

true false

What will the following lines of code print? System.out.println(1 + 5 < 3 + 7);System.out.println( (2 + 2) >= 2 + 3);

compilation fails

What will the following method return if called with an argument of 7? public int transformNumber(int n) {int radix = 2;int output = 0;output += radix * n;radix = output /radix;if(output<14){return output;}else{output = output * radix /2;return output;}else {return output/2;}}

has params no params

What will the following program print when run without any command line argument? public class TestClass {public static void main(String[] args) {boolean hasParams = (args == null ? false : true);if (hasParams) { System.out.println("has params");} { System.out.println("no params");}}}

-10

What will the following program print when run? public class Operators {public static int operators() {int x1 = -4;int x2 = x1--;int x3 = ++x2;if (x2 > x3) { --x3;} else { x1++;}return x1 + x2 + x3;}public static void main(String[] args) {System.out.println(operators());}}

false false

What will the following program print? public class TestClass{static boolean b;static int[] ia = new int[1];static char ch;static boolean[] ba = new boolean[1];public static void main(String args[]) throws Exception{boolean x = false;if( b ){x = ( ch == ia[ch]);}else x = ( ba[ch] = b );System.out.println(x+" "+ba[ch]);}}

3

What will the following program print? class LoopTest{public static void main(String args[]) {int counter = 0;outer:for (int i = 0; i < 3; i++) { middle: for (int j = 0; j < 3; j++) { inner: for (int k = 0; k < 3; k++) { if (k - j > 0) { break middle; } counter++; } }}System.out.println(counter);}}

19

What will the following program print? class Test{public static void main(String args[]){int var = 20, i=0;do{ while(true){ if( i++ > var) break; }}while(i<var--);System.out.println(var);}}

It will not compile

What will the following program print? public class TestClass{public static void main(String[] args){for : for(int i = 0; i< 10; i++){for (int j = 0; j< 10; j++){ if ( i+ j > 10 ) break for;}System.out.println( "hello");}}}

6

What will the following program print?class Test{public static void main(String args[]){int c = 0;boolean flag = true;for(int i = 0; i < 3; i++){ while(flag){ c++; if(i>c || c>5) flag = false; }}System.out.println(c);}} This type of question contains radio buttons and checkboxes for selection of options.

5 6

What will the following program snippet print?int i=0, j=11;do{if(i > j) { break; }j--;}while( ++i < 5);System.out.println(i+" "+j);

There is nothing wrong with the code

What, if anything, is wrong with the following code?void test(int x){switch(x){case 1:case 2:case 0:default :case 4:}}

sizeof <<< mod equals

Which of the following are NOT valid operators in Java?Each correct answer represents a complete solution. Choose all that apply.

|| &&

Which of the following are also called as "short circuiting logical operators"? Each correct answer represents a complete solution. Choose all that apply.

It can iterate over an array or a collection but not a Map Using an enhanced for loop prevents the code from going into an infinite loop You cannot find out the number of the current iteration while iterating

Which of the following are true about the enhanced for loop? Each correct answer represents a complete solution. Choose all that apply.

! ~ & %=

Which of the following are valid operators in Java? Each correct answer represents a complete solution. Choose all that apply.

1 4 5

Which of the following code snippets will print exactly 10? Each correct answer represents a complete solution. Choose all that apply. 1. Object t = new Integer(106); int k = ((Integer) t).intValue()/10; System.out.println(k);2. System.out.println(100/9.9);3. System.out.println(100/10.0);4. System.out.println(100/10);5. System.out.println(3 + 100/10*2-13);

1 , 2, 4

Which of the following four constructs are valid? 1. switch(5){default :}2. switch(5){default : break;}3. switch(8);4. int x = 0;switch(x){}

Objects

Which of the following input types cannot be used as a test in the case statement?

The condition expression in an if statement can contain method calls. If a and b are of type boolean, the expression (a = b) can be used as the condition expression of an if statement. Only expressions which evaluate to a boolean value can be used as the condition in an if statement.

Which of the following statements are true? Each correct answer represents a complete solution. Choose three.

do while

Which of the following statements first executes the code and then evaluates the condition?

break without a label, can occur only in a switch, while, do, or for statement.

Which of the following statements regarding 'break' and 'continue' are true?

for( ; ; )

Which of the followingfor statements represents an infinite loop?

int 1 = 9; System.outprintln(s == i); Integer i = 9; System.out.println(s == i);

Which of the given lines can be inserted at //1 of the following program?Each correct answer represents a complete solution. Choose all that apply.public class TestClass{ public static void main(String[] args){ short s = 9; //1 }}

line 4

Which of the lines will cause a compile time error in the following program? public class MyClass{public static void main(String args[]){char c;int i;c = 'a';//1i = c; //2i++; //3c = i; //4c++; //5}}

switch expression of type int and case label value of type char

Which of these combinations of switch expression types and case label value types are legal within a switch statement?

4, 5

Which of these for statements are valid? 1. for (int i=5; i=0; i--) { }2. int j=5;for(int i=0, j+=5; i<j ; i++) { j--; }3. int i, j;for (j=10; i<j; j--) { i += 2; }4. int i=10;for ( ; i>0 ; i--) { }5. for (int i=0, j=10; i<j; i++, --j) {;}

| %

Which operators will always evaluate all the operands? Each correct answer represents a complete solution. Choose two.

default

Which statement will be called if no case statement matches the value?

1 will be the part of the output 3 will be the part of the output

Which statements about the output of the following programs are true? Each correct answer represents a complete solution. Choose all that apply. public class TestClass{public static void main(String args[ ] ){int i = 0 ;boolean bool1 = true;boolean bool2 = false;boolean bool = false;bool = (bool2 & method1("1")); //1bool = (bool2 && method1("2")); //2bool = (bool1 | method1("3")); //3bool = (bool1 || method1("4")); //4}public static boolean method1(String str){System.out.println(str);return true;}}

double taxRate = grossIncome<=18000? 0 : (grossIncome<=36000)? .1 : .2; double taxRate = 0;if(grossIncome>36000) taxRate = .20;if(grossIncome>18000 && grossIncome<=36000) taxRate = .10; double taxRate = 0;taxRate = grossIncome>18000?grossIncome<=36000?.1:.2:0

You are writing a piece of code that determines tax rate on a given grossIncome. The tax rate is to be computed as follows - If grossIncome is less than or equals to 18000, taxRate is 0. If grossIncome is more than 18000 but less than or equal to 36000, taxRate is 10% If grossIncome is more that 36000, taxRate is 20%. Which of following code fragments do it correctly? Each correct answer represents a complete solution. Choose three.


Conjuntos de estudio relacionados

Chapter 24: The Digestive System

View Set

Med Surg 2 Final (Ch's 9-12, 47)

View Set

Management Chapter 1 Practice Questions

View Set

ATI RN Maternal Newborn practice 2023A

View Set