0% found this document useful (0 votes)
403 views6 pages

CSCI250 - Exam 2 - V1-Solution

This document contains instructions and questions for Exam II for the course CSCI250 Introduction to Programming at Lebanese International University. It is divided into two parts. Part 1 contains 6 multiple choice questions worth a total of 30 points. Part 2 contains two programming problems worth 35 points each. The exam is scheduled for Friday May 6, 2022 from 8:00-9:15 PM. Students must read the instructions carefully and are not allowed to cheat. Lateness will result in point deductions and no exams will be accepted after 15 minutes late.

Uploaded by

Ruba Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
403 views6 pages

CSCI250 - Exam 2 - V1-Solution

This document contains instructions and questions for Exam II for the course CSCI250 Introduction to Programming at Lebanese International University. It is divided into two parts. Part 1 contains 6 multiple choice questions worth a total of 30 points. Part 2 contains two programming problems worth 35 points each. The exam is scheduled for Friday May 6, 2022 from 8:00-9:15 PM. Students must read the instructions carefully and are not allowed to cheat. Lateness will result in point deductions and no exams will be accepted after 15 minutes late.

Uploaded by

Ruba Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CSCI250 Exam II Spring 2021-2022

Lebanese International University - All Campuses


School of Arts and Sciences - Department of Computer Science
Course Name : Introduction to Programming Course Code : CSCI250
Date : Friday May 6, 2022 Time : 20:00-21:15

Problem# Grade Total Grade


Part 1
/100
Problem 1 /30

Part 2
Problem 1 /35
Problem 2 /35

 Read each question carefully before answering.


 Cheating in any form will be punished with a ZERO grade.
 Each minute of lateness will result in a deduction of 1 pt. After 15 minutes
late, NO Exams are accepted and the student must fill in a petition.

Instructions:
1. You can solve the questions using the Netbeans IDE if you want.
2. Then upload one file (Word, text, or PDF) containing all answers.
3. For questions 2 and 3 you can upload java files instead if you want.
Page 1 of 6
CSCI250 Exam II Spring 2021-2022

PART 1

Problem #1: [30 pts: 5 pts for each] Answer the following questions.
Answer
1) Which of the following statements a. int c = 20 + (int) (Math.random()* 100);
generates a random integer between
b. int c = 20 + (Math.random()* 100);
20 and 100 both inclusive?
c. int c = 20 + (Math.random()* 81);
d. int c = 20 + (int) (Math.random()* 81);

2) An else or else-if statement in Java


cannot exist alone without an if a. True
statement. b. False

3) How many times this for-loop will a. zero times


repeat?
b. infinite number of times
int i=20
c. 20 times
While (i>0)
System.out.println("CSCI250"); d. 1 time
i++ ;

4) How may for-loop be an infinite loop?


a. No loop condition
b. Forget the loop counter update
c. Loop condition is never reached
d. All of the above
5) Which of the for-loop statements is a. for(int i = 0; i <= 10; i++)
System.out.println(i);
equivalent to the below do-while loop?
int i = 0;
do{ b. for(int i = 0; i < 10; )
System.out.println(i); System.out.println(i++);
i++ ;
c. for(int i = 0; i > 10; i++)
}while(i < 10); System.out.println(i);

d. for(int i = 1; i <= 10; i++)


System.out.println(i);

6) Update the below code to output the int i = 0, count=1;


while(i <=120) {
even values of i, but three numbers if(count%3==0)
per line. System.out.println(i);
int i = 0; else
while(i <=120); { System.out.print(i+ " ");
System.out.println(i); i+=2;count++;
i++; }
}

Page 2 of 6
CSCI250 Exam I SOLUTION Spring 2020-2021

PART 2

Problem #1: [35 pts]


Write a program that prompt the user to enter 2 items, if the user purchases bread and butter he will have
10 % discount. If the user purchases butter and flour he will get 15% discount. Otherwise, there is no
discount. Your program should accept only 2 items and each item to be purchased only 1 time.

Name Item Price


Bread 1 20
Flour 2 25
Butter 3 30

Sample run1:
Please enter your 2 items: 1 3
Your purchase is:
bread butter
You should pay: 45.0

Sample run2:
Please enter your 2 items: 2 3
Your purchase is:
flour butter
You should pay: 46.75

Sample run3:
Please enter your 2 items: 1 2
Your purchase is:
bread flour
You should pay: 45.0

Sample run4:
Please enter your 2 items: 2 2
An item can be purchased only 1 time

Sample run5:
 Please enter your 2 items: 3 1
Your purchase is:
butter bread 
You should pay: 45.0

Page 3 of 6
CSCI250 Exam I SOLUTION Spring 2020-2021
import java.util.Scanner; //1pt

public class Problem1 {


public static void main(String[] args) {
Scanner input=new Scanner(System.in); //1pt
System.out.print("Please enter your 2 items:"); //1pt
int item1=input.nextInt();//1pt
int item2=input.nextInt();//1pt

if(item1==item2 ) //2pts
System.out.println("An item can be purchased only 1 time");//1pt
else{//2pts
System.out.println("Your purchase is:"); //1pt
double cost=0; //1pt
if(item1==1&&item2==2) //1pt
{ System.out.println("\tBread Flour");//1pt
cost=45; //1pt
}
else if(item1==1&&item2==3) //1pt
{ System.out.println("\tBread Butter");//1pt
cost=50-50*0.1; //2pts
}
else if(item1==2&&item2==1) //1pt
{ System.out.println("\tFlour Bread");//1pt
cost=45; //1pt
}
else if(item1==2&&item2==3) //1pt
{ System.out.println("\tFlour Butter");//1pt
cost=55-55*0.15; //2pts
}
else if(item1==3&&item2==1) //1pt
{ System.out.println("\tButter Bread");//1pt
cost=50-50*0.1; //2pts
}
else if(item1==3&&item2==2) //1pt
{ System.out.println("\tButter Flour");//1pt
cost=55-55*0.15; //2pts
}

System.out.println("You should pay: "+cost); //1pt


}
}
}

Page 4 of 6
CSCI250 Exam I SOLUTION Spring 2020-2021

PART 2
Problem #2: [35 pts]
Write a java program that helps the Lebanese scout to create an online tombola (lottery). Your program
should generate a random number between 10 and 99 using Math.random(). It should then ask the user to
enter 2 numbers each of 1 digit only (0 to 9) and compares these digits with the random number. If the 2
digits match the same placed digits in the number, your program outputs “Congratulations, You win the
tombola” and the random number. The user is allowed to repeat his guess for 10 times maximum.
For example, if the program generates 73 and the user enters 7 and 3, then the user wins. If the user enters
3 and 7, the user loses the round.

Sample Run 1:
Enter 2 digits: 1 6
Wrong, 9 tries left

Enter 2 digits: 3 5
Wrong, 8 tries left

Enter 2 digits: 1 6
Wrong, 7 tries left

Enter 2 digits: 9 1
Congratulations, You win the tombola, the random number is 91 !
Sample Run 2:
Enter 2 digits: 1 9
Wrong, 9 tries left

Enter 2 digits: 1 2
Wrong, 8 tries left

Enter 2 digits: 2 6
Wrong, 7 tries left

Enter 2 digits: 1 6
Wrong, 6 tries left

Enter 2 digits: 1 9
Wrong, 5 tries left

Enter 2 digits: 1 2
Wrong, 4 tries left

Enter 2 digits: 1 9
Wrong, 3 tries left

Enter 2 digits: 1 3
Wrong, 2 tries left

Enter 2 digits: 9 0
Wrong, 1 tries left

Enter 2 digits: 9 8
Wrong, 0 tries left
You lost, the random number is 45 !
Page 5 of 6
CSCI250 Exam I SOLUTION Spring 2020-2021
import java.util.Scanner; //1pt
public class Program2 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);//1pt
int r=10 + (int) (Math.random()* 90); //3pts
int count=10, d1, d2, n1, n2; //3pts
do{ //1pt
System.out.print("Enter 2 digits: ");//1pt
n1=input.nextInt();//1pt
n2=input.nextInt();//1pt
//extract digits from r
d1=r/10; //3pts
d2=r%10; //3pts
if(n1!=d1 || n2!=d2) //4pts
{ count--;//2pts
System.out.println("Wrong, "+count+" tries left"); //1pt
}
else //1pt
count=0; //1pt
}while(count>0); //3pts
if(d1==n1 && d2==n2) //1pt
{System.out.print ("Congratulations, You win the tombola");//1pt
System.out.println(", the random number is "+r+" !"); //1pt
}
else//1pt
System.out.println("You lost, the random number is "+r+" !");
//1pt

}
}

Page 6 of 6

You might also like