Professional Documents
Culture Documents
Assignment Week 1
Assignment Week 1
package quiz.java;
import java.util.Scanner;//imports the java.util.scanner for detecting data entries.
public class week1_quiz {
public static void main(String[] args) {
// TODO Auto-generated constructor stub
int score = 0; //initialize the variable score to zero
String answer;
Scanner answer1 = new Scanner(System.in);
double result; // it is a declaration statement for the variable
result
System.out.print("Week 1: java programing multiple choice Quiz
Questions\n");
System.out.print("please answer by typing A,B,C,D for the corresponding
questions and answers.\n");
System.out.println(" ");
System.out.print("1. A Java program must have at least one\n"+
"A. Class definition \n"+
"B. Variable\n"+
"C. Comment\n"+
"D. System.out.println(); statement\n" );
answer =answer1.nextLine();
if (answer.equalsIgnoreCase("A")) { //while ignoring case if the
answer is "A" it executes the succeeding print statement.
System.out.println("correct, the answer is A\n");
score++;} //adds value one to the initialized value of
the variable score.
else
{System.out.print("incorrect, the correct answer is A\n");}
// if the- if- condition is not met, the print statement gets executed.
Scanner answer2 = new Scanner(System.in);
System.out.print("2. Which of the following is not mandatory in
variable declaration?\n"+
"A. semicolon\n"+
"B. an identifier\n"+
"C. an assignment\n"+
"D. a data type\n" );
answer =answer2.nextLine();
if (answer.equalsIgnoreCase("C")) {
System.out.println("correct, the answer is C\n");
score++ ;}
else
if (answer.equalsIgnoreCase("B")||answer.equalsIgnoreCase("A")||
answer.equalsIgnoreCase("D"))
//if the answer from the choices is not correct, I used the ‘or’ operator along
with .equalsignorecase() to execute the succeeding print statement if any of the
contiond is fulfilled.
{System.out.print("incorrect, the correct answer is C\n");}
else
if (!(answer.equalsIgnoreCase("A"))||!
(answer.equalsIgnoreCase("B"))||!(answer.equalsIgnoreCase("C"))||!
(answer.equalsIgnoreCase("D"))) //if the answer is not included in the list of
choices by using the or operator the next print statement gets executed.
{System.out.println("incorrect,please enter a valid answer\n");}
//As you can see from the snipped png file on the next page, it works correctly and
displays your final score along with its respective percentage.
Reference:
Eck, D. J. (2022). Introduction to programming using java version 9, JavaFX edition.