QUESTIONS WITH CORRECT
ANSWERS GRADED A+
◍ What are the two parts of an if statement?.
Answer: A condition and a body
◍ To count the number of words and then display it, which code below could
be used?a. word_count=len(words)print(fThe sentence has {word_count}
word(s).)b. word_count=len(words)print("The sentence has {word_cnt}
word(s).")c. word_count=length()print(f"The sentence has {word_count}
word(s).")d. word_count=len(words)print(f"The sentence has {word_count}
word(s).").
Answer: d. word_count=len(words)print(f"The sentence has {word_count}
word(s).")
◍ Which of the following statements is true about the if statement?.
Answer: The else block is optional.
◍ True or False? A string always contains a single line of text..
Answer: False
◍ Which code below would be correct for splitting a sentence into words to
later be counted?a. words = user_split.(input)b. words = user_input.split()c.
split=user.input()d. words = user_split.input().
Answer: b. words = user_input.split()
◍ Which of the following statements is correct about an if statement?.
Answer: You can omit an else statement if there is no task defined in the
else branch
◍ Which line of code would be used to create a string that contains the second
, half of PythonProgramming?a. second_half = user_input[:midpoint]b.
first_half = user_input[:midpoint]c. first_half = user_input[midpoint:]d.
second_half = user_input[midpoint:].
Answer: d. second_half = user_input[midpoint:]
◍ True or False? The last character of a string s is at position len(s) -1..
Answer: True
◍ Which of the following is the correct syntax for an if statement?.
Answer: if (x < 10) { size = "Small"; }else { size = "Medium"; }
◍ Assuming that the user provides 303 as input, what is the output of the
following code snippet?int x; int y;x = 0;Scanner in = new
Scanner(System.in);System.out.print("Please enter a number: ");y =
in.nextInt();if (y > 300) { x = y; }else { x = 0; }System.out.println("x: " +
x);.
Answer: x: 303
◍ Assuming that the user provides 99 as input, what is the output of the
following code snippet?int a;int b;a = 0;Scanner in = new
Scanner(System.in);System.out.print("Please enter a number: ");b =
in.nextInt();if (b > 300) { a = b; }else { a = 0; }System.out.println("a: " +
a);.
Answer: a: 0
◍ Accessing a single character out of a string is called:a. concatenationb.
indexingc. assignmentd. slicing.
Answer: b. indexing
◍ True or False? ASCII is a standard for representing characters using numeric
codes..
Answer: True
◍ True or False? A string variable cannot be used as an accumulator..
Answer: False
◍ The following code snippet contains an error. What is the error?if (cost >
, 100);{ cost = cost - 10;}System.out.println("Discount cost: " + cost);.
Answer: Logical error: if statement has do-nothing statement after if
condition
◍ What can be done to improve the following code fragment?if ((counter %
10) == 0){ System.out.println("Counter is divisible by ten: " + counter);
counter++;}else{ System.out.println("Counter is not divisible by ten: " +
counter); counter++;}.
Answer: Move the duplicated code outside of the if statement
◍ What kind of operator is the <= operator?.
Answer: Relational
◍ To find the midpoint for slicing, what following code would be correct?a.
midpoint=length(user_input) * 2b. midpoint=len(user_input)/2c.
midpoint=len(user_input)//2d. midpoint=length(user_input) ** 2.
Answer: c. midpoint=len(user_input)//2
◍ Which of the following operators is used as a relational operator?.
Answer: <=
◍ The operator !> stands for.
Answer: this is not an operator in Java
◍ Assuming that a user enters 15 as input, what is the output of the following
code snippet?Scanner in = new Scanner(System.in);System.out.print("Please
enter a number: ");int number = in.nextInt();if (number > 20) {
System.out.println("The number is LARGE!"); } else {
System.out.println("The number is SMALL!");}.
Answer: The number is SMALL!
◍ A successor to ASCII that includes characters from (nearly) all written
languages isa. TELLIb. ISOc. ASCII++ d. Unicode.
Answer: d. Unicode
◍ To display a sentence using f-strings, which code below could be used?a.
print(f"Original sentence: (user_input)")b. print(f"Original sentence: