0% found this document useful (0 votes)
11 views5 pages

CSEN 104: Programming I Winter Semester 2024: Quiz/Assignment 1

This document is a quiz/assignment for the CSEN 104: Programming I course at the German International University. It includes instructions for completing the quiz, programming exercises with specific outputs and error checks, and a Java program task that evaluates a 4-digit integer based on certain conditions. The document outlines the grading criteria for each exercise.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

CSEN 104: Programming I Winter Semester 2024: Quiz/Assignment 1

This document is a quiz/assignment for the CSEN 104: Programming I course at the German International University. It includes instructions for completing the quiz, programming exercises with specific outputs and error checks, and a Java program task that evaluates a 4-digit integer based on certain conditions. The document outlines the grading criteria for each exercise.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

German International University

Faculty of Informatics and Computer Science


Prof. Dr. Slim Abdennadher

CSEN 104: Programming I


Winter semester 2024
Quiz/Assignment 1

Version 2

Name: ............................................................................................

ID: ................................................................................................

TA Name: .........................................................................................

Tutorial Number: ..................................................................................

Instructions: Read carefully before proceeding.

1) Write your name, ID number, TA name, and group number in the provided space above.

2) No books or other aids are permitted for this test.


3) When you are told that time is up, stop working on the test.

Good Luck!

Don’t write anything below ;-)

Exercise 1 2
Possible Marks 10 10 20
Final Marks
CSEN 104: Programming I, Quiz/Assignment 1, Page 1

Exercise 1 (10 Marks)


What is the output after executing the following programs?

• int score = 76;


String grade = "";
if (score >= 90)
grade = "A";
else
if (score >= 80)
{
grade = "B";
else
if (score >= 70)
grade = "C";
}
else
grade = "F";

System.out.println("The grade is: " + grade);

Error. else without if //2 marks

• double num1 = 4.5;


int num2 = 3; byte
num3 = 2; long
num4 = 6; float
num5 = 1.2f;
double totalSum = num1 + num2 + num3 + num4 + num5;
System.out.println("Total Sum = " + totalSum);

Total Sum = 16.7 //2 marks

• int weight = 4;
int deliveryCharge;

switch (weight) {
case 1, 2:
deliveryCharge = 5;
break;
case 3, 4, 5: deliveryCharge
= 10;
case 6, 7, 8, 9, 10:
deliveryCharge = 15;
default: deliveryCharge =-1;
}

if (deliveryCharge == -1)
System.out.println("Package weight not accepted");
else

System.out.println("Delivery Charge: $" + deliveryCharge);

Error. Cannot use comma to combine cases //2 marks


CSEN 104: Programming I, Quiz/Assignment 1, Page 2

• int hour = 14;


boolean isVegetarian = false;
String mealSuggestion;

mealSuggestion = (hour < 12) ? (isVegetarian ? "Vegetarian Breakfast"


: "Breakfast with Eggs") : ((hour < 18) ? (isVegetarian ? "Vegetarian Lunch" :
"Lunch with Chicken") : (isVegetarian ? "Vegetarian Dinner"
: "Dinner with Fish"));

System.out.println("Suggested Meal: " + mealSuggestion);

Suggested Meal: Lunch with Chicken //2 marks

• int x = 9;
int y = 3;

if ((x % y == 0) && (++x > y) || (--y == 2))


System.out.println(x * y);
else
if ((x + y > 10) && (y-- < x) && (++y > 2))
System.out.println(x + y);
else
System.out.println(x - y);

30 //2 marks
CSEN 104: Programming I, Quiz/Assignment 1, Page 3

Exercise 2 (10 Marks)


Write a Java program that prompts the user to enter a 4-digit integer. Use conditional statements to
determine if the number meets the following conditions:

• The sum of the first and last digits should be even. If it is, proceed to the next condition.
Otherwise, display a message indicating the number does not meet the even sum requirement.
• The difference between the second and third digits must be odd. If it is, proceed to the final
condition. Otherwise, display a message indicating the number does not meet the odd difference
requirement.
• The first digit must be greater than or equal to the last digit. If it is, display a message saying the
number meets all conditions. Otherwise, display a message indicating the number does not meet
the final requirement.

Examples:

Input: 4826
Output: The number does not meet the odd difference requirement.

Input: 9452
Output: The number does not meet the even sum requirement.

Input: 8412
Output: The number meets all conditions.

Input: 328
Output: Invalid input. The number is not a 4-digit number.

import java.util.*; // 0.5 mark for import, class name and


main method
public class quiz_1 {
public static void main(String[] args) {
//0.5 mark for scanner
Scanner sc= new Scanner(System.in);
//0.5 mark for input
System.out.println("please enter a 4-digit number");
int x = sc.nextInt();
// 1 mark for checking the error case
if(x > 9999 || x <1000){
System.out.println("Invalid input. The number is not a 4-digit
number");
}
else{
int d1 = x%10; // 0.5 mark
x = x /10; // 0.5 mark
int d2 = x%10; // 0.5 mark
x = x/10; // 0.5 mark
int d3 = x%10; // 0.5 mark
int d4 = x/10; // 0.5 mark
// 1 mark for each condition
if((d1+d4) % 2 ==0){
int diff = d2>d3? d2-d3 : d3-d2; // 0.5 mark
if(diff%2 == 1){
if(d4 > d1){
//1 mark for all print statements
System.out.println("The number meets all
conditions");
}
else{
System.out.println("The number does not meet
the first and the last digits requirement");
}
}
else{
System.out.println("The number does not meet the
odd difference requirement.");
}
}
else{
System.out.println("The number does not meet the even
sum requirement.");
}
}

}
}

You might also like