IF statements and Boolean expressions/For loop

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/69

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

70 Terms

1
New cards

String a = "Hi";

String b = "Hi";

String c = a;

String d = "Hi!"

String e = newString("Hi")

System.out.println(a == c);

System.out.println(d == b);

System.out.println(a == b);

System.out.println(a == e);

true

false

true

false

2
New cards

a && false == false

a && true == a

a && a == a

a && !a == false

a || false == a

a || true == true

a || a == a

a || !a == true

!(!a) == a (The inverse of an inverse is the original value)

a && false == false

a && true == a

a && a == a

a && !a == false

a || false == a

a || true == true

a || a == a

a || !a == true

!(!a) == a (The inverse of an inverse is the original value)

3
New cards
4
New cards

int a = 14;

int b = -15;

int c = 22;

int d = 11;

if(a > 12 && b < -14 && c < d) {

System.out.println("YAY!!!!");

} else if (d - 20 < b) {

System.out.println("GO DO MORE PROGRAMMING!");

} else if(a + 12 >= c) {

System.out.println("This is vexing......");

} else {

System.out.println("This is very fun!");

}}

What is the output for the code above?

This is vexing......

5
New cards

for(int i = 0; i < 10; i++) {

if(i % 2 == 0) {

System.out.println("This is my favorite case!");

} else if(i + 6 > 12 && i % 3 == 1) {

System.out.println("There once was a pool in the back yard!");

} else {

System.out.println("Well, at least it is something...");

}

}

This is my favorite case!

Well, at least it is something...

This is my favorite case!

Well, at least it is something...

This is my favorite case!

Well, at least it is something...

This is my favorite case!

There once was a pool in the back yard!

This is my favorite case!

Well, at least it is something...

6
New cards

int i = 45, j = -12;

String s = "This is great!";

String s2 = "Where are you?";

if(s2.charAt(4) == ' ' && (i < 4 || j > -43) && !s2.equals("WOW!!!")) {

System.out.println("Happy days are here again!");

} else if (s2.charAt(5) == 's') {

System.out.println("Well, we are here at least.");

} else if (j + 12 != 0) {

System.out.println("The stock market is up today!");

} else if(i - 20 < 100) {

System.out.println("Where will we be having lunch tomorrow?");

} else {

System.out.println("But here you go!");

}

Where will the group be having lunch tomorrow?

7
New cards

int i = 12, j = -5, k = 103;

if(i < 15 && j >= -4) {

System.out.println("You should take more philosophy classes!");

} else if(!(i < 100 && k > 4)) {

System.out.println("I love medieval studies!");

} else if(!(i == 3 && k == 4)) {

System.out.println("My favorite philosopher is Radulphus Brito, of course!");

} else if(i != 4 && k == 102) {

System.out.println("My favorite philosopher is Johann Fichte.");

} else {

System.out.println("I am going to cut the grass tomorrow.");

}

My favorite philosopher is Radulphus Brito, of course!

8
New cards

char c = 'M';

String s = "James of Metz";

String s2 = "Albert of Sazony";

int i = 14;

if(i > 34 || c < 'B') {

System.out.println("I love Duns Scotus!");

} else if(s.charAt(9) == c) {

System.out.println("I read William of Ockham yesterday!");

} else if(!(i < 100 || s.equals("James of Metz"))) {

System.out.println("Well, I call William of Ockham 'Bill' because we're BFFs.");

} else if(s2.equalsIgnoreCase("Al")) {

System.out.println("I was reading Simon of Faversham, and he is very logical.");

} else {

System.out.println("There is nobody like Peter of Spain for a good logic read.");

}

I read William of Ockham yesterday!

9
New cards

What does the expression (7 != 14) evaluate to?

True

10
New cards

boolean x;

int y;

x = false;

y = 10;

x = !(y < 10 || y > 10);

System.out.println ( x );

What is printed?

True

11
New cards

What does this statement return?

int a = 2

int b = 3

a == b

Possible Answers:

false

12
New cards

Which is correct?

String a = "2"

String b = "2"

Is a == b correct? Or is a.equals(b) correct?

Possible Answers:

a.equals(b)

13
New cards

int currentYear = 2016;

bool leapYear;

if (currentYear % 4 == 0) {

leapYear = true;

} else {

leapYear = false;

}

febDays = leapYear ? is it ?28 or 29days

28

14
New cards

int j=6;

int k=0;

int l=2;

int c = (j|k) & l;

What is the value of c?

2

15
New cards

int num1 = 9;

int num2 = 5;

if (num > num2)

{ System.out.println((num1 + num2) % num2);

}

else

{ system.out.println((num1 - num2) % num2);

}

What is printed as a result of executing the code segment?

A) 0 ; b) 2 ; c) 4 ; d)9 ; E) 14

C

16
New cards

int var=0;

int array[4];

for (int j=0; j<=4;j++)

{

var=var+1;

}

What is the value of var?

5

17
New cards

#include

using namespace std;

int main()

{

int i=0;

int sum =0;

for(i;i<4;i+=2)

{

sum=sum+i;

i--;

}

return 0;

}

What is the value of i and sum?

i=3,sum=6

18
New cards

Suppose you have the following code:

public static void main(String[] args) {

int a =2;

if (a%2==0)

System.out.println("Hello World");

else

System.out.println("Hi");

}

Hello World

19
New cards

int k = arr.length;

for (int i = -1; i

k-1

20
New cards

public void countStatements() {

int a = 10, b = 15, c = 7;

// Start

for(int i = 0; i < a; i += 2) {

b--;

System.out.print("*");

}

for(int i = b; i >=0; i--) {

for(int j = 0; j < c; j++) {

System.out.print("*");

}

}

// End

}

21
New cards

1.

int j=6;

int k=0;

int l=2;

int c = (j|k) & l;

What is the value of c?

2

22
New cards

int value = initvalue;

if (value > 10)

if (value >15 )

value = 0;

else

value = 1;

system.out.println("Value = " + value);

under which of the condition below will this code segment print value = 1;

A) initvalue = 8;

B) initvalue = 12;

C) initvalue = 20;

d)initvalue = 0 will always be printed.

e) never code will not compile.

B

23
New cards

In the following equation, considering the boolean variables x, y, and z, which of the following combinations of values will evaluate to true?

(x\,\&\&\,!y)\,||\,((!x\,||\,z)\,\&\&\,!(y\,||\,z))

x == true, y == false, z == false

24
New cards

#include

using namespace std;

bool bigger(int x, int y)

{

if (x>y)

return true;

else

return false;

}

int main()

{

bool test;

test!=bigger(10,7);

cout<

0

25
New cards

2 laws of De Morgan

!True = ?

!False = ?

!(a && b) is equivalent to =?

!(a || b) is equivalent to =?

True

False

!a || b

!a && !b

26
New cards

if(!(x>=0) AND y<=100) convert it into something that does not have not's

if (!(x>=0) or !(Y<=100)

!(x>=0) = x<0

and

!(y<=100) = y>100

27
New cards

boolean a = True, b = false

if ( / missing code/)

system .out.println("Nice Job");

else

System.out.println("nicer job");

which or the following could be used to replace /missing code/ so that output of this block of code is "Nicer job"

a) a && !b

b) !a || b

c) !a && b

b & C only

28
New cards

(!p && !q) || !(P || q)

a) The expression always evaluates to true

b) The expression always evaluates to False

c) The expression always evaluates to P is false

d) The expression always evaluates to true when ever q is false

e) The expression evaluates to false whenever p and q have opposite truth values.

E is correct

29
New cards

For (int i = 209; i > 0; i /= 3)

{if (i % 2 ==0)

system.out.println( i + " ");

}

A) 200 66 22 7 2

B)66 22 7 2

c)200 66 22 2

d) 200 66 22

e) 7

C

30
New cards

int i = x % 50;

if x is a positive integer, which of the following could not be the value of i after the statement above execute?

A) 0

b) 10

c) 25

d) 40

e) 50

E

31
New cards

Assume that a, b,c have been declared and initialized with int values, The expression

! (a>b || b<=c)

is equivalent to which of the following?

A) a >b && b <= c

B) a<=b || b>c

c) a<= b && b>c

d)a = c

e)a=c

C

32
New cards

which condition statement is equivalent to the expression shown below?

if ((temp > 80) && !(80 < temp))

A) if (temp == 80)

B) if (temp !=80)

C) if ((temp >= 80) || (temp >80))

D) false

E) True

D

33
New cards

A high school class places students in a course based on their average from a pervious course. Students that have an average 92 or above are placed in an honors level course. Students that have an average below 74 are placed in a remedial level course. All other students are placed in a regular level course. Which of the the following statements correctly places the students based on the their average?

Code A)

if (average > = 92)

level = "honors";

if (average < 74)

level = " remedial";

else

level = " regular";

}

Code B)

if (average > = 92)

level = "honors";

if (average > = 74)

level = " regular";

else

level = " remedial";

}

Code C)

if (average > = 92)

level = "honors";

else if ((average >= 74) && (average <92)))

level = " regular";

else

level = " remedial";

}

A) Code A only

B) Code B only

C) Code C only

D) Code A and B only

E) code B & C only.

E

34
New cards

Assume that x,y and z have been declared as follows:

boolean x = true;

boolean y = false;

boolean z = true ;

which of the following expression evaluates to true?

A) x && y && z

b) x && y || z && y

c) ! (x && y) || z

d) !(x|| y ) && z

e) x && y || !z

C

35
New cards

Consider the following code segment

int amount = initValue;

if (amount >= 0)

if (amount < = 0)

system.out.println("**");

else

system.out.println("%%");

under what condition will this code segment print %%?

A ) initValue = -5

B) initValue = 0

C) initValue = 5

D) initValue = 10

E) initValue = 15

E

36
New cards

Which segment of code will correctly print the two strings name1 and name2 in alphabetical order

I ) if (name1 < name 2)

system.out.println( name1 + " " + name2);

else

system.out.println(name2 + " " + name1);

II)

if (name1.compareTo(name 2))

system.out.println( name1 + " " + name2);

else

system.out.println(name2 + " " + name1);

III)if (name1.compareTo(name 2) < 0)

system.out.println( name1 + " " + name2);

else

system.out.println(name2 + " " + name1);

A) I only

B) II only

C) III only

D) II and III only

E) I,II, and III only

C

37
New cards

computing the results of complex logical expression using Demorgan's law :

int a = 2, b = 3;

boolean result1 = !(b == 3 && a < 1); what is the output

True /False?

True

38
New cards

computing the results of complex logical expression using Demorgan's law :

int a = 2, b = 3;

boolean result2 = !(a != 2 || b < = 4);; what is the output

True /False?

False

39
New cards

int count = 0;

int total = 0;

boolean result = (count != 0 && total/count > 0); what is the output, True/False?

False

40
New cards

What is the output of the below code?

for (int i = 0 ; i <3 ; I ++)

{ system.out.printlin("Hello");

}

Hello

Hello

Hello

41
New cards

for (int j = 9; j > 6 ; J--)

{ system.out.println( j + " ");

system.out.println(10 - j);

}

9 1

8 2

7 3

42
New cards

for (int k = 1; k <= 4;K++)

{ system.out.println(k);

}

system.out.println(k*10);

Compile - time error K cannot be resolved to a variable

43
New cards

int i ;

for (i = 1; i < 5; i ++)

{ system.out.println(i);

}

system.out.println(i);

1

2

3

4

5

44
New cards

nested loops:

for (int i = 0,i < 3; i ++)

{

for ( int j =1,j <5,j++)

{

system.out.println(i + " " + j);

}

}

0 1

0 2

0 3

0 4

1 1

1 2

1 3

1 4

2 1

2 2

2 3

2 4

45
New cards

for (int x = 4; x > 0 ; X --)

{

for (int y = 9 ; y < = 15 ; y ++)

{ total time the loop executes

}

}

28 times the loop executes.

46
New cards

consider the following code segment

int count = 1;

int value = 31;

while (value >= 10)

{ value = value - count;

count = count + 3;

}

system.out.println(value);

What is printed as a result of executing the code segment ?

A) 4

B) 9

c) 10

D)13

E)31

B

47
New cards

Consider the following code segment.

int count = 5;

for ( int = 3 ; i <7; i = i+2)

{ count + = 1;

}

system.out.println(count);

What is printed as a result of executing the code segment?

A) 5

B) 7

C)9

D)13

E)20

D

48
New cards

Consider the following code segment

int incr = 1;

for (int i = 0; i < 10; I + = incr)

{ system.out.println (i - incr + " ");

}

what is printed as a result of executing the code segment?

A) -1 0 2 5

B) -1 0 3 7

C) -1 1 4 8

D) 0 2 5 9

E)0 1 3 7

A

49
New cards

Which of the following code segments will print exactly eight " $" symbols?

I ) for ( int i =0 ; i < 8; i ++)

{ system.out.print("$");

II) int i = 0;

while (i < 8)

{ system.out.println("$");

I++;

}

III) for ( int i = 7; i <= 30; i+=3)

{ system.out.println("$");

}

A) I only

B) II only

C) I and II only

D) I and III only

E) I , II, and III

E

50
New cards

Consider the following code segments.

int a = / value supplied with in program/

int b = / value supplied with in progoram /

if ( / missing condition /)

{ int result = a +b;

}

which of the following should replace / missing condition / to ensure that a+b will not be too large to be held correctly in result?

A) a + b < Intetger.MAX_Value

B) a +b <= Integer.MAX_Value

C)Integer.MIN_Value + a <= b

D)Integer.MIN_Value - a <= b

E)Integer.MIN_Value - a >= b

E

51
New cards

Consider the following code segment

for (int j = 0; j < 10 ; j++)

{ for ( int k = 10 ; k > j ; k --)

{ systme.out.println("*");

}

}

How many "*" symbols are printed as a result of executing the code segment ?

A) 5, B)10,C)45,D)55,E)100

D

52
New cards

Consider the following code segment.

boolean b1 = true ;

boolean b2 = true;

int x = 7;

while ( b1 || b2)

{ if (x > 4)

{ b2 = !b2 ;

}

else

{ b1 = !b1;

}

x--;

}

system.out.print(x);

What is printed as a result of executing the code segment?

A) 3,B)4,c)6,D) 7,E) Nothing will be printed ; infinite loop.

A

53
New cards

Which of the following three code segments will produce the same output?

I) int i = 1;

while

(i <12)

{ system.out.print(i + " ");

I*=2;

}

II) for (int i = 4 ; i > 0 ; i --)

{ system.out.print((4-i) * 2 +1 + ' ");

}

III) for ( int i =0 ; i <4 ; i ++)

{ system.out.print((int)Math.pow(2.i) + " "}'

}

A ) I and II only

B) II and III only

C) I and III only

D) I,II, and III

E) all three outputs are different.

C

54
New cards

Consider the following code segment

double count = 6.0;

for (int num = 0;num < 5; num ++)

{ if (count ! = 0 && num/count > 0)

{ count - = num;

}

}

system.out.println(count);

What is printed as a result of executing the code segment?

A) 0.0 B) -4.0 C)5 D) the program will end successfully , but nothing will be printed.

E) Nothing will be printed. Run-time error : Arithemeti cexception.

A

55
New cards

int n = 0;

while (n <20)

{ system.out.print(n % 4 + " ");

if ( n % 5 == 2)

{ n + = 4 ;

} else

{ n +=3;

}

}

what is printed as a result of executing the code segment?

A) 0 3 2 1 0 0 3

B) 0 0 3 2 2 1

c) 0 3 2 1 0 0

D) 0 0 3 2 2 1 0

E Many numbers will be printed ; infinite loop.

A

56
New cards

consider the following code segment

double tabulate = 100.0;

int repeat = 1;

while ( tabulate > 20 )

{ tabulate = tabulate - math.pow(repeat ,2);

repeat ++;

}

system.out.println(tabulate);

what is printed as a result of executing the code segment?

a) 20.0 B) 6.0 C) 19.0 D) 9.0 E) -40.0

C

57
New cards

What does the following segment of java code print out?

double x = 4 4.5;

int y = (int)x ;

system.out.println(x+" "+y);

4.5 , 4

58
New cards

what does the following segment of java code print out?

int x = 7, y = 3;

int z = x/y + x%y;

if (z ==y)

x++;

System.out.println(X+" "+y+" "+z);

8 3 3

59
New cards

What does the following segment of java code print out?

for (int i =1;i <30; i = i+3)

{ if (i %2 == 0)

system.out.print((i/2) + " ");

}

System.out.println();

2,5,8,11,14

60
New cards

What does the following segment of jave code do in general?

scanner stdin = new scanner (system.in);

System.out.println("enter n ,");

int n = stdin.nextint();

int total = 0;

while ( n>0)

{ total = total + n %10;

n=n/10;

}

system.out.println(total);

This segment of code prints out the sum of the digits of the original value of n entered by the user.

61
New cards

which of the following Boolean expression are equivalent ? ( Assume that x and y are intergers variables that have been initialized with the intended values)

i) ((x>0) && (Y>0)) || ((X>0) && (Y<0))

ii) X! = Y

iii) (x>0) && (y!=0)

iv) (x>0) && (x+y ! =x)

A ) i & ii

B) i and iii

C) i , iii, iv

D) ii, iii,iv

E ) All 4 are different , logically.

C

62
New cards

The Boolean expression !(A||B) is equvalent to which of the following?

A) !A || !B

B) !A || B

C) !A && !B

D)!A && B

E) None of the Above

C

63
New cards

What values are stored in x and y after execution of the following program segment.?

int x = 30, y = 40;

if (X> = 0)

{ if (x<=100)

{ y = x*3;

if (y<50)

x/=10;

}

Else

y = x*2;

}

else

y =-x;

A) x=30,y = 90

B ) x = 30 Y = -30

C) x =30, y= 60

D)x=3,y=-3

E)x=30,y= 40

B

64
New cards

Which of the following will evaluate to true only if boolean expressed A,B,C are all false?

A) ! A && !(B && !C)

B) !A || !B ||!C

C)! (A || B || C)

D) ! ( A && B && B && C)

E) !A || !(B || C)

65
New cards

Assume that a and b are integers . The boolean expression

!(a <= b) && (a* b >0)

will always evaluate to true given that

A) a = b

B) a > b

C) a < b

D) a> b and b >0

E) a > b and b < 0

66
New cards

Given that a, b, and c are integers, consider that Boolean expression

(a < b) || ! ((c ++ a * b) && (c< a))

Which of the following will guarantee that the expression is true ?

A) C < a is false

B) C < a is true

C) a < b is false

D) c === a * b is true

E) c == a * b is true , and c < a is true

67
New cards

In the following code segment, you may assume that a,b and n are all type int

if (a ! = b && n/ (a-b) > 90)

{

/ statement 1/

}

else

{

/ statement 2 /

}

/ statement 3/

A) What will happen if a == b is false?

B) / statement 2 /

C)Either ? Statement 1 / or ? Statement 2 / will be executed

D) A compile time error will occur

E) An exception will be thrown.

68
New cards

Given that n and count are both of type int, which statement is true about the following code segments?

I ) for ( count = 1 ; count < = n ; count ++)

system.out.println(count);

II) count =1

while (count < = n)

{ system.out.println(count);

count++

}

A ) I and II are exactly equivalent for all input value n.

B) I and II are exactly equivalent for all input values n > = 1, but differe when n<= 0.

C) I and II are exactly equivalent only when n= 0.

D) I and II are exactly equivalent only when n is even.

E) I and II are not equivalent for any input values of n.

69
New cards

int x = 10 , y = 0;

while (x> 5)

{

y = 3;

while (y

70
New cards

What output will be produced by this code segmetn ?

for int( i = 5;i>=1;i--)

{ for (int j = 1; j>=1;j--)

system.out.print(2* j-1);

system.out.println();

}

A) 9 7 5 3 1

9 7 5 3

9 7 5

9 7

9

B) 9 7 5 3 1

7 5 3 1

5 3 1

3 1

1

C) 9 7 5 3 1

7 5 3 1 -1

5 3 1 -1 -3

3 1 -1 -3 -5

1 -1 -3 -5 -7

D) 1

1 3

1 3 5

1 3 5 7

1 3 5 7 9

E) 1 3 5 7 9

1 3 5 7

1 3 5

1 3

1