SAMPLE EXAMS QUESTIONS
An if statement is a construct that enables a program to specify alternative paths of execution. E.g. if ( condition to test) { statement(s) to execute } What happens if the condition is false? You must combine relational or conditional operators with selection statements E.g. <, >, <=, >=, &&, ||, ==, ! Selection Statements (IF-ELSE) if ( condition to test ) { ...
Quick Rules
-
Time limit: 50 minutes
-
Multiple attempts are not allowed
-
All questions must be answered to submit
Share Quiz
Quiz Questions Preview
Question 1
What does an if statement do in a program?
Explanation
An if statement allows the program to choose different paths of execution based on whether a condition is true or false.
Question 2
What valid syntax does an if-else statement follow?
Explanation
The correct syntax for an if-else statement includes both conditions properly placed within parentheses and blocks delimited by braces.
Question 3
What value does a boolean variable represent in Java?
Explanation
A boolean variable in Java can represent two possible states: true or false.
Question 4
When is a 'break' statement typically used in a loop?
Explanation
'Break' is used in loops to immediately terminate the loop when executed.
Question 5
What do switch statements allow you to do?
Explanation
Switch statements allow testing a variable against multiple possible values in a structured way.
Question 6
A student is developing a program that evaluates their score based on the UEW grading scheme. They want to implement an if-else-if statement that assigns a grade according to the following criteria: if Total_Score is greater than or equal to 80, assign 'A'; if Total_Score is between 70 and 79, assign 'B'; if Total_Score is between 60 and 69, assign 'C'; otherwise, assign 'F'. If the student mistakenly uses an if-else structure that only checks greater than and less than conditions incorrectly, what is likely to happen when executing the grade assignment?
Explanation
The correct answer is that the program will only assign grades based on passing and failing scores, missing middle grades. The if-else structure must appropriately capture all ranges of scores, and if it incorrectly checks the conditions, only the extremes might be evaluated, ignoring the middle ranges. Other options reflect misconceptions about how if-else statements function and do not effectively analyze the consequences of condition coverage in selection statements.
Question 7
Consider a scenario where you have a nested if statement to check if a student is from UEW, in the Department of ICT Education, at Level 200, and in your specific group. If the condition for checking the group is placed incorrectly inside the nested structure, which outcome is likely?
Explanation
The correct outcome is that the program might not display anything if the group check fails. If the group condition is incorrectly placed inside of nested if statements such that not all conditions are fulfilled, it could lead to not executing the corresponding display statement. Other options either misrepresent how nested if statements work or create false expectations about successful outputs with incomplete conditions.
Question 8
[Case Scenario] The IT department in a university developed a program to automatically grade student scores based on a predefined scheme. The grading criteria are simple: if a student's Total_Score is above 90, they receive an 'A'. If it's between 80 and 89, they get a 'B'. If it's between 70 and 79, they get a 'C'. Scores below 70 receive a 'D'. The developers chose to implement an if-else-if statement to determine each student's grade using their Total_Score variable. For example, the code snippet looks like this: if (Total_Score > 90) { System.out.println("Grade: A"); } else if (Total_Score >= 80) { System.out.println("Grade: B"); } else if (Total_Score >= 70) { System.out.println("Grade: C"); } else { System.out.println("Grade: D"); } However, the developers are concerned about how well this approach scales with the addition of new grading criteria for future semesters. Question: Based on the grading scheme, what would be the output of the code if a student has a Total_Score of 85?
Explanation
In the provided grading system, a Total_Score of 85 clearly fits into the grading range for a B. The if-else-if statement allows the program to check each condition sequentially until it finds one that is true. Hence, once it checks if the score is greater than 90 or in the other ranges, it correctly classifies 85 as a B.
Question 9
[Case Scenario] A developer is working on a program to identify students from the University of Education, Winneba (UEW), who are in the Department of ICT Education, currently in Level 200, and belong to a specific study group. The program uses a nested if statement to check these conditions and, if all are true, it prints "We are learning Java". The provided code is as follows: if (isUEW && isICT && isLevel200) { if (isInGroup) { System.out.println("We are learning Java"); } } The developer is eager to ensure that only students who satisfy all conditions receive this message. However, they are unsure whether the current structure is the most efficient way to implement it. Question: What happens if the first condition, isUEW, is false?
Explanation
In the nested if structure, if any of the outer conditions evaluate to false, the inner condition checks are not executed. Therefore, if isUEW is false, the program will skip the entire inner block, meaning no output will be generated.
Question 10
[Case Scenario] A team of software engineers is working on an ATM simulation program that utilizes a do-while loop to allow users to access services repeatedly until they choose to exit. The implementation is as follows: do { // Display services } while (userWantsToContinue); They are currently considering how to best provide an option for the user to return to the service menu immediately after completing a transaction without needing to restart the program. The condition 'userWantsToContinue' is based on user input regarding whether they want to continue accessing services. The developers want to ensure this loop functions effectively and considers the edge cases where users might want to exit immediately after transactions. Question: If the user decides to exit after one transaction, what effect does this have on the execution of the do-while loop?
Explanation
In a do-while loop, the code inside the loop executes at least once regardless of the condition. If the user opts to exit after the first transaction, the condition 'userWantsToContinue' will be evaluated as false after that iteration, causing the loop to terminate after a single execution.