Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Week 1: programming cs1102 Assignemnt:

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");}

Scanner answer3 = new Scanner(System.in);


System.out.print("3. Java programs are\n"+
"A. Faster than others \n"+
"B. Platform independent\n"+
"C. Not reusable\n"+
"D. Not scalable\n" );
answer =answer3.nextLine();
if (answer.equalsIgnoreCase("B")) {
System.out.print("correct, the answer is B\n");
score++ ;
}
else
if (answer.equalsIgnoreCase("A")||answer.equalsIgnoreCase("C")||
answer.equalsIgnoreCase("D"))
System.out.print("incorrect, the correct answer is B\n");
else
System.out.print("incorrect, please enter a correct
answer: the correct answer is B\n");
Scanner answer4 = new Scanner(System.in);
System.out.print("4. All java classes are derived from\n"+"A.
java.lang.Class\n"+"B. java.util.Name\n"+"C. java.lang.Object\n"+"D. java.awt.Window\
n");
answer = answer4.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"))
{System.out.print("incorrect, the correct answer is C\n");}
else
System.out.println("incorrect, please enter a correct
answer");}
Scanner answer5 = new Scanner(System.in);
System.out.print("5. By using ………………. you can force immediate
termination of a loop, by passing the conditional expression and any remaining code
in the body of the loop\n"+
"A. Break\n"+
"B. Continue\n"+
"C. Terminate\n"+
"D. Loop Close\n" );
answer = answer5.nextLine();
answer5.close();
switch (answer) { // i used a switch statement, as it can be applied
for fixed set of values that we can compare against like comparing single variables
against multiple values
case "A": // while switching the value of the variable answer over the
various cases, for the first case, if the answer/input is "A", the succeeding print
statement gets executed and the value of the variable score gets updated.
System.out.print("correct, the answer is A\n.");
score++;
break; // breaks the program out of the loop
case "a": // as switch statement are case sensitive and use "== "
operator to compare respective values, i used a redundant code to cover both the
upper and lower case scenarios for each valid multiple choice enteries.
System.out.print("correct, the answer is A\n.");
score++;
break;
case "B":
System.out.print("incorrect, the correct answer is A\n");
break;
case "b":
System.out.print("incorrect, the correct answer is A\n");
break;
case "C":
System.out.print("incorrect, the correct answer is A\n");
break;
case "c":
System.out.print("incorrect, the correct answer is A\n");
break;
case "D":
System.out.print("incorrect, the correct answer is A\n");
break;
case "d":
System.out.print("incorrect, the correct answer is A\n");
break;
default:
System.out.println("incorrect, please insert a correct value");}
answer5.close();
System.out.println(" ");
System.out.println(" ");
System.out.print("***********you have answered "+ score + " questions
correctly***********\n");
result = (( score* 100)/5);
System.out.println(" your final result in percentage out of 100%
is " + result+ " %");
}
}

//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.

You might also like