SlideShare a Scribd company logo
1 of 16
Download to read offline
Which if statement below tests if letter holds R? (letter is a char variable)
if ( letter == "R")
if ( letter >= 'R')
if ( letter == R)
if ( letter = 'R')
if ( letter == 'R')
What are if statements used for in programs?
Repeating commands
Storing data
Numeric calculations
Numeric casts
Making decisions
The following if statement tests the rainfall in New York’s Central Park during the months of
June, July and August.
if (low <= rain && rain <= high)
System.out.println("Rainfall amount is normal.");
else
System.out.println("Rainfall amount is abnormal.");
It could be replaced with:
I.
if (rain >= low)
{
if (rain <= high)
System.out.println("Rainfall amount is normal.");
}
else
System.out.println("Rainfall amount is abnormal.");
II.
if (rain >= low)
{
if (rain <= high)
System.out.println("Rainfall amount is normal.");
else
System.out.println("Rainfall amount is abnormal.");
}
else
System.out.println("Rainfall amount is abnormal.");
III.
if (rain >= low)
System.out.println("Rainfall amount is normal.");
else if (rain <= high)
System.out.println("Rainfall amount is normal.");
else
System.out.println("Rainfall amount is abnormal.");
I only
II only
III only
II or III
I, II or III
What is output by the following code?
int x = 36 % 8;
if (x >= 10)
System.out.println( 1);
else if (x >= 8)
System.out.println( 2);
else if (x >= 6)
System.out.println( 3);
else if ( x >= 4)
System.out.println( 4);
else
System.out.println( 5);
1
2
3
4
5
Consider the code:
if ( y == 0 || x * y > 10)
Which of the following is an example of short circuit evaluation?
if x * y > 10 is false it evaluates y ==0
if x * y > 10 is false it doesn't evaluate y ==0
if y == 0 is false it doesn't evaluate x * y > 10
if y == 0 is true it doesn't evaluate x * y > 10
if y == 0 is false it evaluates x * y > 10
The following truth table matches which boolean condition?
A && ( A || B)
A || ( !A && !B)
A && ( A && B)
!A && ( A || !B)
A || ( A || B)
Consider the code:
if (a < b && c != d)
Which of the following is an example of short circuit evaluation?
if a < b is true it doesn't evaluate c != d
if a < b is false it doesn't evaluate c != d
if c != d is false it evaluates a < b
if c != d is true it doesn't evaluate a < b
if a < b is true it evaluates c != d
! ( x < y && w == z) is the same as which boolean expression?
x <= y && w == z
x >= y || w != z
x <= y || w != z
x <= y && w != z
x < y && w != z
Assume that x and y are boolean variables and have been properly initialized.
!(x || y) || (x || y)
The result of evaluating the expression above is best described as:
always true
always false
true only when x is true and y is true
true only when x and y have the same value
true only when x and y have different values
What is output to the screen by the following code?
int c = 2;
while (c < 6)
{
System.out.print((int)Math.pow (-1, c)+" ");
c++;
}
-1 1 -1 1 -1 1 -1
1 -1 1 -1
-1 1 -1 1 -1 1
1 1 1 1 1 1
-1 -1 -1 -1 -1 -1
How many times will the following loop repeat?
int num = 49;
while (num > 0)
{
if (num % 2 == 0)
num++;
else
num--;
}
20
21
22
23
Infinite Loop
What is output to the screen by the following code?
int num = 1987;
while (num >0)
{
num = num / 10;
System.out.print(num % 10 + " ");
}
8 9 1 0
198 19 1 0
19 1 0 0
7 8 9 1
The loop will not terminate
The following loop is intended to print the even numbers from 20 to 26:
int x = 20;
while (x < 26)
{
System.out.print(x);
x++;
}
What would you need to change in order for the code to work correctly?
The x++ needs to be x += 2
The x++ needs to be x += 2 and the x < 26 needs to be <=
The x < 26 needs to be <=
It needs an if statement: if (x % 2 == 0)
Nothing, the code works as written.
The following code is intended to input three integers and print the average:
System.out.println( "Please enter three integers: ");
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
System.out.println( "The average is: " + 1.0 * a + b + c/3);
What is a potential problem with the code as written?
It needs ( ) so the order of operations happens correctly.
No correction needed, the code will work as written.
It should be divided by 2, not 3.
It should use scan.nextDouble instead of scan.nextInt.
The parentheses are not needed and will cause a mathematical error.
Which of the following needs a cast?
char stored in an int variable
double stored in an int variable
char stored in a String variable
int stored in a double variable
char stored in a double variable
Consider the following code segment:
int c = 1;
while (c <= 10)
{
if ( c % 3 == 1)
System.out.print(c + " ");
c++;
}
Which of the following produce the exact same output?
I.
int c = 1;
while (c <= 10)
{
c++;
if ( c % 3 == 1)
System.out.print(c + " ");
}
II.
int c = 1;
while (c <= 10)
{
System.out.print(c + " ");
c += 3;
}
III.
int c = 0;
while (c <= 10)
{
c++;
if ( c % 3 == 1)
System.out.print(c + " ");
}
I only
II only
III only
II and III only
I, II and III
Which of the following correctly gives random numbers between -10 and 10 inclusive?
int n = (int)(Math.random() * 20)-10;
int n = (int)(Math.random() * 21)-10;
int n = (int)(Math.random() * 11)-20;
int n = (int)(Math.random() * 10)-20;
int n = (int)(Math.random() * 10)-21;
Consider the following code:
int count = 4;
while (count <= 7)
{
count++;
System.out.print(count + " ");
}
What are the first and last numbers output?
4 7
4 8
5 7
5 8
Nothing is output.
Consider the following code:
int diff = 0;
if (Math.abs(num1 - num2) == (num1 - num2))
diff = num1 - num2;
else if (Math.abs(num2 - num1) == (num2 - num1))
diff = num2 - num1;
Which of the following will have the exact same result?
I.
int diff = Math.abs( num1 ) - num2;
II.
int diff = Math.abs( num1 - num2);
III.
int diff = Math.abs( num2 - num1);
I only
II only
III only
II and III only
I, II, and III
Of the following code blocks, which one correctly executes exactly two commands only when
the condition is true?
A.
B.
C.
B and C but not A
A and C but not BAB________111101010001
Solution
1.
The if statement which will test the condition that the letter will hold a char value ‘R’ is given as
follows:
if(letter == ‘R’)
Since, letter is a char variable. Thus, the first choice is incorrect, third and fourth are not valid if
statements. The second if statement is not checking the required condition.
Hence, the correct choice is if(letter == ‘R’).
2.
The if statements are used to make decision like structure in a program. Hence, the correct choice
is e. Making decisions.
3.
The given if statement can be replaced by nested if statements as follows:
if(rain >= low)
{
if(rain <= high)
{
System.out.println("Rainfall amount is normal.");
}
else
{
System.out.println("Rainfall amount is abnormal.");
}
}
else
{
System.out.println("Rainfall amount is abnormal.");
}
The above if-else structure can be explained as follows:
The firt if statement checks whether the rainfall is less than or equal to low rainfall. If this
condition satisfies, then the flow will go to the inside if block, otherwise, the else part executed.
The second if statement will check the second condition of the original if statement i.e., if(rain
<= high).
Hence, the correct choice is II only.
4.
The first statement in the given code will assign the remainder of 36/8 to the variable x. The
correct condition will be else if(x >= 4) according to the present value of x. The fourth else if
will be executeda nd prints he value 4.
Hence, the correct choice is 4.
5.
The given if statement will execute if block, when one of the two conditions is true or both
conditions are true. If the value of y is 0, then y == 0 will be true and if condition is satisfied.
Hence, the correct choice is if y == 0 is true it doesn’t evaluate x * y > 10 because x * 0 can
never be greater than 10, it will be 0.
6.
The correct boolean conditio which satisfies the given truth table is given as follows:
A || (!A && !B)
The output for the input A = 1 And B = 1 can be calculated as follows:
1 || (0 && 0)
1 || 0
1
The output 1 is matched with the output given in the table.
The output for the input A = 1 And B = 0 can be calculated as follows:
1 || (0 && 1)
1 || 0
1
The output 1 is matched with the output given in the table.
The output for the input A = 0 And B = 1 can be calculated as follows:
0 || (1 && 0)
0 || 0
0
The output 0 is matched with the output given in the table.
The output for the input A = 0 And B = 0 can be calculated as follows:
0 || (1 && 1)
0 || 1
1
The output 1 is matched with the output given in the table.
Hence, the correct choice is A || (!A && !B).
7.
The short circuit evaluation says that in case of and (&&) if first expression before && is false,
then the expression after (&&) will not be evaluated. If a < b is not satisfied, then c != d will
never be evaluated.
Hence, the correct choice is if a < b is false it doesn’t evaluate c != d.
8.
The ! operator will reverse the result of the condition (x < y && w == z). The given if condition
can be replaced by a condition in which < will be replaced by >=, && operator will be replaced
by || operator, and w == z will be replaced with !=.
Hence, the correct choice is x >= y || w != z.
9.
If (x || y) condition is true (1), then the given expression will result as true 1. This can be
explained as follows:
Assume x || y = 1
!(x || y) || (x || y)
!(1) || (1)
0 || 1 = 1
Hence, if the condition x || y is 1, then the result will also be 1 (true).
Assume x || y = 0
!(x || y) || (x || y)
!(0) || (0)
1 || 0 = 1
Hence, if the condition x || y is 0, then the result will be 1 (true).
Hence, the result will be true in each case. The correct choice is always true.
10.
The output of the given code can be explained as follows:
The value of c is 2. So, the while condition c < 6 = 2 < 6 is satisfied and the while block will be
executed and the result of (-1)^2 = 1 is printed. Now, the value of c is incremented by one and
becomes 3.
The value of c is 3. So, the while condition c < 6 = 3 < 6 is satisfied and the while block will be
executed and the result of (-1)^3 = -1 is printed. Now, the value of c is incremented by one and
becomes 4.
The value of c is 4. So, the while condition c < 6 = 4 < 6 is satisfied and the while block will be
executed and the result of (-1)^4 = 1 is printed. Now, the value of c is incremented by one and
becomes 5.
The value of c is 5. So, the while condition c < 6 = 5 < 6 is satisfied and the while block will be
executed and the result of (-1)^5 = -1 is printed. Now, the value of c is incremented by one and
becomes 6.
The value of c is 6. So, the while condition c < 6 = 6 < 6 is not satisfied and the while block is
not executed. The program stops and the required output on the console will be 1 -1 1 -1.
Hence, the correct choice is 1 -1 1 -1.
11.
The execution of given while loop can be explained as follows:
The value of num is 49 initially and the condition in the while num > 0 is satisfied. The while
loop is executed and the if condition num % 2 == 0 is not satisfied. The else part executed and
value of num is decremented by one and becomes 48.
The value of num is 48 and the condition in the while num > 0 is satisfied. The while loop is
executed and the if condition num % 2 == 0 is satisfied. The value of num is incremented by one
and becomes 49 again.
This value of num will become 48 and 49 alternatively and the loop will never ends.
Hence, the correct choice is infinite loop.
12.
The output of the given code can be explaias follows:
The value of num is 1987 initially. The condition of while num > 0 is satisfied and the while
block is executed. The num becomes num / 10 i.e., 1987 / 10 = 198 and the value of 198 % 10 =
8 is printed.
The value of num is 198. The condition of while num > 0 is satisfied and the while block is
executed. The num becomes num / 10 i.e., 198 / 10 = 19 and the value of 19 % 10 = 9 is printed.
The value of num is 19. The condition of while num > 0 is satisfied and the while block is
executed. The num becomes num / 10 i.e., 19 / 10 = 1 and the value of 1 % 10 = 1 is printed.
The value of num is 1. The condition of while num > 0 is satisfied and the while block is
executed. The num becomes num / 10 i.e., 1 / 10 = 0 and the value of 0 % 10 = 0 is printed.
The value of num is 0. The condition of while num > 0 is not satisfied and the program stops.
The required output will be 8 9 1 0.
Hence, the correct choice is 8 9 1 0.
13.
The present code will print the numbers from 20 to 25. The modifications in the code to print
even numbers from 20 to 26 is shown as follows:
The condition in the while x < 26 needs to be replaced with x <= 26 otherwise, 26 will not be
printed. The increment of x in the while loop should be changed such that x is incremented by 2
in each iteration.
Initially in the while loop x = 20 willl be printed and now x becomes 22 by x+ = 2. Now the
value 22 is printed and value of x becomes 24 and so on.
Hence, the correct choice is that the x++ needs to be x += 2 and the x < 26 needs to be x <= 26.
14.
There is an logical error in the last statement of the code. It will not give correct average of three
numbers because the sum of three numbers is not divided by 3 instead, division of third number
by 3 is added to the sum of first two numbers.
The whole sum should be divided by 3 to get the correct average. The parentheses should be
added to the statement a + b + c/3 like (a + b + c)/3 and now the correct average value will be
printed.
Hence, the correct choice is that it needs () so the order of operations happens correctly.
15.
The type casting is used to change the data type of the right hand side value to match with the
data type of the left hand side value. If a double value is stored in a int variable, then it needs
type casting to change the double to int.
int a = (int)8.5;
Hence, the correct choice is double stored in the an int variable.
16.
The code with the same output of the given code will be as follows:
The II code will generate same output because the output values are generated by the gap of 3.
The output will be 1, 4, 7, 10. The III code will also give same values because it increments the
value of c first and then prints the value of c if c % 3 == 1.
Hence, the correct choice is II and III only.
17.
The correct stratement which will generate a random value between -10 and 10 inclusively is
shown as follows:
int n = (int)(Math.random() * 20) – 10;
The above statement will select a random number between -10 to 10 because Math.random() *
20 will generate a random number less than 20 and (Math.random() * 20) -1 will generate a
random number -10 to 10.
Hence, the correct choice is int n = (int)(Math.random() * 20) – 10;.
18.
The first number which is printed by the given code will be 5 because the condition in the while
loop is satisfied and value of count becomes 5 by the statement count++ in the while loop.
The value of count is incremented in each iteration till it becomes greater than 7. So, the last
value will be 8.
Hence, the correct choice is 5 8.
19.
The given code will always print the positive value of variable diff because if the first condition
in the if statement satisfied, then the difference will be num1 – num2 and it will be positive
otherwise, num2 – num1 and it is also positive.
This if-else if structure can be replaced by the statements II and III becausboth statements will
give same output as of given code.
Hence, the correct choice is II and III only.
20.
The B code is not correct because there is a need of curly bracket block for the if statement as
there are two statements in the if block. The A and C code will give two commands.
Hence, the correct choice is A and C but not B.

More Related Content

Similar to Which if statement below tests if letter holds R (letter is a char .pdf

CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
Sanjjaayyy
 
Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_if
TAlha MAlik
 
ExamName___________________________________MULTIPLE CH.docx
ExamName___________________________________MULTIPLE CH.docxExamName___________________________________MULTIPLE CH.docx
ExamName___________________________________MULTIPLE CH.docx
gitagrimston
 
Module code 124ms Tajvinder Virdee Sets and Log.docx
Module code 124ms  Tajvinder Virdee  Sets and Log.docxModule code 124ms  Tajvinder Virdee  Sets and Log.docx
Module code 124ms Tajvinder Virdee Sets and Log.docx
moirarandell
 

Similar to Which if statement below tests if letter holds R (letter is a char .pdf (20)

Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And EngineersAnswers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview Questions
 
L05if
L05ifL05if
L05if
 
Es272 ch2
Es272 ch2Es272 ch2
Es272 ch2
 
Lecture 3 and 4.pptx
Lecture 3 and 4.pptxLecture 3 and 4.pptx
Lecture 3 and 4.pptx
 
Bit manipulation
Bit manipulationBit manipulation
Bit manipulation
 
C/C++ programming language Something Great/tutorialoutletdotcom
C/C++ programming language Something Great/tutorialoutletdotcomC/C++ programming language Something Great/tutorialoutletdotcom
C/C++ programming language Something Great/tutorialoutletdotcom
 
C++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPEC++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPE
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 
Basic Theory (FE)
Basic Theory (FE)Basic Theory (FE)
Basic Theory (FE)
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
 
introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptx
 
c++ Data Types and Selection
c++ Data Types and Selectionc++ Data Types and Selection
c++ Data Types and Selection
 
Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_if
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in c
 
ExamName___________________________________MULTIPLE CH.docx
ExamName___________________________________MULTIPLE CH.docxExamName___________________________________MULTIPLE CH.docx
ExamName___________________________________MULTIPLE CH.docx
 
Module code 124ms Tajvinder Virdee Sets and Log.docx
Module code 124ms  Tajvinder Virdee  Sets and Log.docxModule code 124ms  Tajvinder Virdee  Sets and Log.docx
Module code 124ms Tajvinder Virdee Sets and Log.docx
 
Control All
Control AllControl All
Control All
 

More from aniarihant

Im suppose to make a preOrder transversal method so that it performs.pdf
Im suppose to make a preOrder transversal method so that it performs.pdfIm suppose to make a preOrder transversal method so that it performs.pdf
Im suppose to make a preOrder transversal method so that it performs.pdf
aniarihant
 
Ecosystem ecologyCompare and contrast abiotic gradients in a tempe.pdf
Ecosystem ecologyCompare and contrast abiotic gradients in a tempe.pdfEcosystem ecologyCompare and contrast abiotic gradients in a tempe.pdf
Ecosystem ecologyCompare and contrast abiotic gradients in a tempe.pdf
aniarihant
 
Bill is outside on a snowy day without a warm coat. His skin is al.pdf
Bill is outside on a snowy day without a warm coat. His skin is al.pdfBill is outside on a snowy day without a warm coat. His skin is al.pdf
Bill is outside on a snowy day without a warm coat. His skin is al.pdf
aniarihant
 
6. A more in depth look at how to recognize “true adaptation”. Skim .pdf
6. A more in depth look at how to recognize “true adaptation”. Skim .pdf6. A more in depth look at how to recognize “true adaptation”. Skim .pdf
6. A more in depth look at how to recognize “true adaptation”. Skim .pdf
aniarihant
 

More from aniarihant (20)

Im suppose to make a preOrder transversal method so that it performs.pdf
Im suppose to make a preOrder transversal method so that it performs.pdfIm suppose to make a preOrder transversal method so that it performs.pdf
Im suppose to make a preOrder transversal method so that it performs.pdf
 
Give an explanation of the pedigree below. SolutionThe inherit.pdf
Give an explanation of the pedigree below.  SolutionThe inherit.pdfGive an explanation of the pedigree below.  SolutionThe inherit.pdf
Give an explanation of the pedigree below. SolutionThe inherit.pdf
 
elena ents you to impact into MAt ab SolutionThree fu.pdf
elena ents you to impact into MAt ab SolutionThree fu.pdfelena ents you to impact into MAt ab SolutionThree fu.pdf
elena ents you to impact into MAt ab SolutionThree fu.pdf
 
Ecosystem ecologyCompare and contrast abiotic gradients in a tempe.pdf
Ecosystem ecologyCompare and contrast abiotic gradients in a tempe.pdfEcosystem ecologyCompare and contrast abiotic gradients in a tempe.pdf
Ecosystem ecologyCompare and contrast abiotic gradients in a tempe.pdf
 
Compare and contrast ClearCase and ClearQuest.SolutionClearcas.pdf
Compare and contrast ClearCase and ClearQuest.SolutionClearcas.pdfCompare and contrast ClearCase and ClearQuest.SolutionClearcas.pdf
Compare and contrast ClearCase and ClearQuest.SolutionClearcas.pdf
 
Assume that you have a garden and some pea plants have solid leaves a.pdf
Assume that you have a garden and some pea plants have solid leaves a.pdfAssume that you have a garden and some pea plants have solid leaves a.pdf
Assume that you have a garden and some pea plants have solid leaves a.pdf
 
According to NASA GISS measurements, the years 2005 and 2010 are tied.pdf
According to NASA GISS measurements, the years 2005 and 2010 are tied.pdfAccording to NASA GISS measurements, the years 2005 and 2010 are tied.pdf
According to NASA GISS measurements, the years 2005 and 2010 are tied.pdf
 
A drawer contains 6 blue and 4 white socks. Two of the socks are chos.pdf
A drawer contains 6 blue and 4 white socks. Two of the socks are chos.pdfA drawer contains 6 blue and 4 white socks. Two of the socks are chos.pdf
A drawer contains 6 blue and 4 white socks. Two of the socks are chos.pdf
 
Bill is outside on a snowy day without a warm coat. His skin is al.pdf
Bill is outside on a snowy day without a warm coat. His skin is al.pdfBill is outside on a snowy day without a warm coat. His skin is al.pdf
Bill is outside on a snowy day without a warm coat. His skin is al.pdf
 
As a network administrator, what are some of the options you have fo.pdf
As a network administrator, what are some of the options you have fo.pdfAs a network administrator, what are some of the options you have fo.pdf
As a network administrator, what are some of the options you have fo.pdf
 
A concert has three pieces of music to be played before intermission.pdf
A concert has three pieces of music to be played before intermission.pdfA concert has three pieces of music to be played before intermission.pdf
A concert has three pieces of music to be played before intermission.pdf
 
5 of 50 students are female. If I randomly selects 10 students witho.pdf
5 of 50 students are female. If I randomly selects 10 students witho.pdf5 of 50 students are female. If I randomly selects 10 students witho.pdf
5 of 50 students are female. If I randomly selects 10 students witho.pdf
 
Write a assembly program for IA-32 to count up the number of even num.pdf
Write a assembly program for IA-32 to count up the number of even num.pdfWrite a assembly program for IA-32 to count up the number of even num.pdf
Write a assembly program for IA-32 to count up the number of even num.pdf
 
Which of the following is a weighted average of some elements having .pdf
Which of the following is a weighted average of some elements having .pdfWhich of the following is a weighted average of some elements having .pdf
Which of the following is a weighted average of some elements having .pdf
 
Which of the following statements isare true about the graph of the .pdf
Which of the following statements isare true about the graph of the .pdfWhich of the following statements isare true about the graph of the .pdf
Which of the following statements isare true about the graph of the .pdf
 
35. 2H H2 has E0 of -0.42 and NoihN2 has Eo of +0.74. Which is the .pdf
35. 2H H2 has E0 of -0.42 and NoihN2 has Eo of +0.74. Which is the .pdf35. 2H H2 has E0 of -0.42 and NoihN2 has Eo of +0.74. Which is the .pdf
35. 2H H2 has E0 of -0.42 and NoihN2 has Eo of +0.74. Which is the .pdf
 
6. A more in depth look at how to recognize “true adaptation”. Skim .pdf
6. A more in depth look at how to recognize “true adaptation”. Skim .pdf6. A more in depth look at how to recognize “true adaptation”. Skim .pdf
6. A more in depth look at how to recognize “true adaptation”. Skim .pdf
 
What does antibody titer meanSolution It is a method for t.pdf
What does antibody titer meanSolution  It is a method for t.pdfWhat does antibody titer meanSolution  It is a method for t.pdf
What does antibody titer meanSolution It is a method for t.pdf
 
What role do regular expressions typically play in language compiler.pdf
What role do regular expressions typically play in language compiler.pdfWhat role do regular expressions typically play in language compiler.pdf
What role do regular expressions typically play in language compiler.pdf
 
What is the role of ribosomes in an operon What is the role of.pdf
What is the role of ribosomes in an operon What is the role of.pdfWhat is the role of ribosomes in an operon What is the role of.pdf
What is the role of ribosomes in an operon What is the role of.pdf
 

Recently uploaded

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Recently uploaded (20)

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 

Which if statement below tests if letter holds R (letter is a char .pdf

  • 1. Which if statement below tests if letter holds R? (letter is a char variable) if ( letter == "R") if ( letter >= 'R') if ( letter == R) if ( letter = 'R') if ( letter == 'R') What are if statements used for in programs? Repeating commands Storing data Numeric calculations Numeric casts Making decisions The following if statement tests the rainfall in New York’s Central Park during the months of June, July and August. if (low <= rain && rain <= high) System.out.println("Rainfall amount is normal."); else System.out.println("Rainfall amount is abnormal."); It could be replaced with: I. if (rain >= low) { if (rain <= high) System.out.println("Rainfall amount is normal."); } else System.out.println("Rainfall amount is abnormal."); II. if (rain >= low) { if (rain <= high) System.out.println("Rainfall amount is normal."); else System.out.println("Rainfall amount is abnormal."); }
  • 2. else System.out.println("Rainfall amount is abnormal."); III. if (rain >= low) System.out.println("Rainfall amount is normal."); else if (rain <= high) System.out.println("Rainfall amount is normal."); else System.out.println("Rainfall amount is abnormal."); I only II only III only II or III I, II or III What is output by the following code? int x = 36 % 8; if (x >= 10) System.out.println( 1); else if (x >= 8) System.out.println( 2); else if (x >= 6) System.out.println( 3); else if ( x >= 4) System.out.println( 4); else System.out.println( 5); 1 2 3 4 5 Consider the code: if ( y == 0 || x * y > 10) Which of the following is an example of short circuit evaluation? if x * y > 10 is false it evaluates y ==0 if x * y > 10 is false it doesn't evaluate y ==0
  • 3. if y == 0 is false it doesn't evaluate x * y > 10 if y == 0 is true it doesn't evaluate x * y > 10 if y == 0 is false it evaluates x * y > 10 The following truth table matches which boolean condition? A && ( A || B) A || ( !A && !B) A && ( A && B) !A && ( A || !B) A || ( A || B) Consider the code: if (a < b && c != d) Which of the following is an example of short circuit evaluation? if a < b is true it doesn't evaluate c != d if a < b is false it doesn't evaluate c != d if c != d is false it evaluates a < b if c != d is true it doesn't evaluate a < b if a < b is true it evaluates c != d ! ( x < y && w == z) is the same as which boolean expression? x <= y && w == z x >= y || w != z x <= y || w != z x <= y && w != z x < y && w != z Assume that x and y are boolean variables and have been properly initialized. !(x || y) || (x || y) The result of evaluating the expression above is best described as: always true always false true only when x is true and y is true true only when x and y have the same value true only when x and y have different values What is output to the screen by the following code? int c = 2; while (c < 6) { System.out.print((int)Math.pow (-1, c)+" ");
  • 4. c++; } -1 1 -1 1 -1 1 -1 1 -1 1 -1 -1 1 -1 1 -1 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 How many times will the following loop repeat? int num = 49; while (num > 0) { if (num % 2 == 0) num++; else num--; } 20 21 22 23 Infinite Loop What is output to the screen by the following code? int num = 1987; while (num >0) { num = num / 10; System.out.print(num % 10 + " "); } 8 9 1 0 198 19 1 0 19 1 0 0 7 8 9 1 The loop will not terminate The following loop is intended to print the even numbers from 20 to 26: int x = 20; while (x < 26)
  • 5. { System.out.print(x); x++; } What would you need to change in order for the code to work correctly? The x++ needs to be x += 2 The x++ needs to be x += 2 and the x < 26 needs to be <= The x < 26 needs to be <= It needs an if statement: if (x % 2 == 0) Nothing, the code works as written. The following code is intended to input three integers and print the average: System.out.println( "Please enter three integers: "); int a = scan.nextInt(); int b = scan.nextInt(); int c = scan.nextInt(); System.out.println( "The average is: " + 1.0 * a + b + c/3); What is a potential problem with the code as written? It needs ( ) so the order of operations happens correctly. No correction needed, the code will work as written. It should be divided by 2, not 3. It should use scan.nextDouble instead of scan.nextInt. The parentheses are not needed and will cause a mathematical error. Which of the following needs a cast? char stored in an int variable double stored in an int variable char stored in a String variable int stored in a double variable char stored in a double variable Consider the following code segment: int c = 1; while (c <= 10) { if ( c % 3 == 1) System.out.print(c + " "); c++; }
  • 6. Which of the following produce the exact same output? I. int c = 1; while (c <= 10) { c++; if ( c % 3 == 1) System.out.print(c + " "); } II. int c = 1; while (c <= 10) { System.out.print(c + " "); c += 3; } III. int c = 0; while (c <= 10) { c++; if ( c % 3 == 1) System.out.print(c + " "); } I only II only III only II and III only I, II and III Which of the following correctly gives random numbers between -10 and 10 inclusive? int n = (int)(Math.random() * 20)-10; int n = (int)(Math.random() * 21)-10; int n = (int)(Math.random() * 11)-20; int n = (int)(Math.random() * 10)-20; int n = (int)(Math.random() * 10)-21; Consider the following code:
  • 7. int count = 4; while (count <= 7) { count++; System.out.print(count + " "); } What are the first and last numbers output? 4 7 4 8 5 7 5 8 Nothing is output. Consider the following code: int diff = 0; if (Math.abs(num1 - num2) == (num1 - num2)) diff = num1 - num2; else if (Math.abs(num2 - num1) == (num2 - num1)) diff = num2 - num1; Which of the following will have the exact same result? I. int diff = Math.abs( num1 ) - num2; II. int diff = Math.abs( num1 - num2); III. int diff = Math.abs( num2 - num1); I only II only III only II and III only I, II, and III Of the following code blocks, which one correctly executes exactly two commands only when the condition is true? A. B. C. B and C but not A
  • 8. A and C but not BAB________111101010001 Solution 1. The if statement which will test the condition that the letter will hold a char value ‘R’ is given as follows: if(letter == ‘R’) Since, letter is a char variable. Thus, the first choice is incorrect, third and fourth are not valid if statements. The second if statement is not checking the required condition. Hence, the correct choice is if(letter == ‘R’). 2. The if statements are used to make decision like structure in a program. Hence, the correct choice is e. Making decisions. 3. The given if statement can be replaced by nested if statements as follows: if(rain >= low) { if(rain <= high) { System.out.println("Rainfall amount is normal."); } else { System.out.println("Rainfall amount is abnormal."); } }
  • 9. else { System.out.println("Rainfall amount is abnormal."); } The above if-else structure can be explained as follows: The firt if statement checks whether the rainfall is less than or equal to low rainfall. If this condition satisfies, then the flow will go to the inside if block, otherwise, the else part executed. The second if statement will check the second condition of the original if statement i.e., if(rain <= high). Hence, the correct choice is II only. 4. The first statement in the given code will assign the remainder of 36/8 to the variable x. The correct condition will be else if(x >= 4) according to the present value of x. The fourth else if will be executeda nd prints he value 4. Hence, the correct choice is 4. 5. The given if statement will execute if block, when one of the two conditions is true or both conditions are true. If the value of y is 0, then y == 0 will be true and if condition is satisfied. Hence, the correct choice is if y == 0 is true it doesn’t evaluate x * y > 10 because x * 0 can never be greater than 10, it will be 0. 6. The correct boolean conditio which satisfies the given truth table is given as follows: A || (!A && !B)
  • 10. The output for the input A = 1 And B = 1 can be calculated as follows: 1 || (0 && 0) 1 || 0 1 The output 1 is matched with the output given in the table. The output for the input A = 1 And B = 0 can be calculated as follows: 1 || (0 && 1) 1 || 0 1 The output 1 is matched with the output given in the table. The output for the input A = 0 And B = 1 can be calculated as follows: 0 || (1 && 0) 0 || 0 0 The output 0 is matched with the output given in the table. The output for the input A = 0 And B = 0 can be calculated as follows: 0 || (1 && 1) 0 || 1 1 The output 1 is matched with the output given in the table. Hence, the correct choice is A || (!A && !B). 7.
  • 11. The short circuit evaluation says that in case of and (&&) if first expression before && is false, then the expression after (&&) will not be evaluated. If a < b is not satisfied, then c != d will never be evaluated. Hence, the correct choice is if a < b is false it doesn’t evaluate c != d. 8. The ! operator will reverse the result of the condition (x < y && w == z). The given if condition can be replaced by a condition in which < will be replaced by >=, && operator will be replaced by || operator, and w == z will be replaced with !=. Hence, the correct choice is x >= y || w != z. 9. If (x || y) condition is true (1), then the given expression will result as true 1. This can be explained as follows: Assume x || y = 1 !(x || y) || (x || y) !(1) || (1) 0 || 1 = 1 Hence, if the condition x || y is 1, then the result will also be 1 (true). Assume x || y = 0 !(x || y) || (x || y) !(0) || (0) 1 || 0 = 1 Hence, if the condition x || y is 0, then the result will be 1 (true). Hence, the result will be true in each case. The correct choice is always true.
  • 12. 10. The output of the given code can be explained as follows: The value of c is 2. So, the while condition c < 6 = 2 < 6 is satisfied and the while block will be executed and the result of (-1)^2 = 1 is printed. Now, the value of c is incremented by one and becomes 3. The value of c is 3. So, the while condition c < 6 = 3 < 6 is satisfied and the while block will be executed and the result of (-1)^3 = -1 is printed. Now, the value of c is incremented by one and becomes 4. The value of c is 4. So, the while condition c < 6 = 4 < 6 is satisfied and the while block will be executed and the result of (-1)^4 = 1 is printed. Now, the value of c is incremented by one and becomes 5. The value of c is 5. So, the while condition c < 6 = 5 < 6 is satisfied and the while block will be executed and the result of (-1)^5 = -1 is printed. Now, the value of c is incremented by one and becomes 6. The value of c is 6. So, the while condition c < 6 = 6 < 6 is not satisfied and the while block is not executed. The program stops and the required output on the console will be 1 -1 1 -1. Hence, the correct choice is 1 -1 1 -1. 11. The execution of given while loop can be explained as follows: The value of num is 49 initially and the condition in the while num > 0 is satisfied. The while loop is executed and the if condition num % 2 == 0 is not satisfied. The else part executed and value of num is decremented by one and becomes 48. The value of num is 48 and the condition in the while num > 0 is satisfied. The while loop is executed and the if condition num % 2 == 0 is satisfied. The value of num is incremented by one
  • 13. and becomes 49 again. This value of num will become 48 and 49 alternatively and the loop will never ends. Hence, the correct choice is infinite loop. 12. The output of the given code can be explaias follows: The value of num is 1987 initially. The condition of while num > 0 is satisfied and the while block is executed. The num becomes num / 10 i.e., 1987 / 10 = 198 and the value of 198 % 10 = 8 is printed. The value of num is 198. The condition of while num > 0 is satisfied and the while block is executed. The num becomes num / 10 i.e., 198 / 10 = 19 and the value of 19 % 10 = 9 is printed. The value of num is 19. The condition of while num > 0 is satisfied and the while block is executed. The num becomes num / 10 i.e., 19 / 10 = 1 and the value of 1 % 10 = 1 is printed. The value of num is 1. The condition of while num > 0 is satisfied and the while block is executed. The num becomes num / 10 i.e., 1 / 10 = 0 and the value of 0 % 10 = 0 is printed. The value of num is 0. The condition of while num > 0 is not satisfied and the program stops. The required output will be 8 9 1 0. Hence, the correct choice is 8 9 1 0. 13. The present code will print the numbers from 20 to 25. The modifications in the code to print even numbers from 20 to 26 is shown as follows: The condition in the while x < 26 needs to be replaced with x <= 26 otherwise, 26 will not be printed. The increment of x in the while loop should be changed such that x is incremented by 2 in each iteration.
  • 14. Initially in the while loop x = 20 willl be printed and now x becomes 22 by x+ = 2. Now the value 22 is printed and value of x becomes 24 and so on. Hence, the correct choice is that the x++ needs to be x += 2 and the x < 26 needs to be x <= 26. 14. There is an logical error in the last statement of the code. It will not give correct average of three numbers because the sum of three numbers is not divided by 3 instead, division of third number by 3 is added to the sum of first two numbers. The whole sum should be divided by 3 to get the correct average. The parentheses should be added to the statement a + b + c/3 like (a + b + c)/3 and now the correct average value will be printed. Hence, the correct choice is that it needs () so the order of operations happens correctly. 15. The type casting is used to change the data type of the right hand side value to match with the data type of the left hand side value. If a double value is stored in a int variable, then it needs type casting to change the double to int. int a = (int)8.5; Hence, the correct choice is double stored in the an int variable. 16. The code with the same output of the given code will be as follows: The II code will generate same output because the output values are generated by the gap of 3. The output will be 1, 4, 7, 10. The III code will also give same values because it increments the value of c first and then prints the value of c if c % 3 == 1.
  • 15. Hence, the correct choice is II and III only. 17. The correct stratement which will generate a random value between -10 and 10 inclusively is shown as follows: int n = (int)(Math.random() * 20) – 10; The above statement will select a random number between -10 to 10 because Math.random() * 20 will generate a random number less than 20 and (Math.random() * 20) -1 will generate a random number -10 to 10. Hence, the correct choice is int n = (int)(Math.random() * 20) – 10;. 18. The first number which is printed by the given code will be 5 because the condition in the while loop is satisfied and value of count becomes 5 by the statement count++ in the while loop. The value of count is incremented in each iteration till it becomes greater than 7. So, the last value will be 8. Hence, the correct choice is 5 8. 19. The given code will always print the positive value of variable diff because if the first condition in the if statement satisfied, then the difference will be num1 – num2 and it will be positive otherwise, num2 – num1 and it is also positive. This if-else if structure can be replaced by the statements II and III becausboth statements will give same output as of given code. Hence, the correct choice is II and III only.
  • 16. 20. The B code is not correct because there is a need of curly bracket block for the if statement as there are two statements in the if block. The A and C code will give two commands. Hence, the correct choice is A and C but not B.