You are on page 1of 4

American University Of Beirut

Department of Computer Science


CMPS201 - LAB 2

Deadline: Friday Midnight Fall 2023 Date: February 5, 2024

Attempt All questions

Question 1
What happens when you cast a double value to an int?

• The decimal part is preserved.


• The decimal part is rounded to the nearest integer smaller than that double (floor of
the number).
• The decimal part is rounded to the nearest integer greater than that double (ceiling
of the number).
• The entire value is set to zero.
• An error occurs.

Question 2
How do you import the Scanner class into your Java program?

• import java.util.Scanner;
• include Scanner;
• require Scanner;
• You don’t need to import it; it’s automatically available.

Question 3
What method creates a Scanner object that reads input from the standard input?

• createScanner()
• new Scanner(System.in);
• readInput()
• inputScanner()

Question 4
How do you read an integer from the standard input using the Scanner class?

• Scanner.nextInt()
• Scanner.readInt()
• Scanner.int()
• Scanner.read()

Question 5
What happens when a program attempts to read an integer using the Scanner’s nextInt()
method but the user feeds in a String instead?

• It reads the first digit of the string as an integer.


• It throws an error.
• It ignores the input and waits for a valid integer.
• It continues reading characters until it finds an integer.

Question 6
What is the difference between pre-increment (++i) and post-increment (i++)?

• They have the same effect.


• Pre-increment increments first, then uses the value, while post-increment uses the
value then increments.
• Pre-increment decrements, while post-increment increments.
• They cannot be used with variables.
• Pre-increment is used for double, while post-increment integers.

Question 7
How do you access individual characters in a string?

• Use the charAt() method with the character index.


• Use square brackets [].
• Use the length() method.
• You cannot access individual characters in a string.

Page 2
Programming Questions

Question 8
Write a program that declares three variables: age (integer), height (double), and name
(string). Initialize age to 25, height to 1.75, and name to your own name. Then, print the
values of these variables on separate lines.

Question 9
Modify the previous program to prompt the user to enter their age and name using the
Scanner class. Store the user’s input in the appropriate variables and print a greeting
message containing their name and age.

Question 10
Write a program that reads a full name from the user using the nextLine() method of the
Scanner class. Extract the first name and last name and prints them separately.

Question 11
Given the following program:
1 public class Test {
2 public static void main ( String [] args ) {
3 long x = 3;
4 byte y = 1;
5 double z = 2.0;
6 int r = x * y / z ;
7 System . out . println ( r );
8 }
9 }

What is the output of the previous JAVA program?


What is the output if line 6 was replaced with:

• int r = (int) x * y / z;
• int r = (int) (x * y / z);
• double r = x * y / z;
• double r = (double) x * y / z;
Question 12
1 import Scanner ;
2 public class R ec ta ng le Pe ri me te r
3 public static void Main ( string [] args ) {
4 Scanner scanner = new Scanner ( System . out );
5 System . out . print ( " Enter the width of the rectangle : " );
6 double width = scanner . nextdouble ();
7 System . out . print ( " Enter the height of the rectangle : " )
8 double height = scanner . nextdouble ();
9 double perimeter = 2 ( width + height );
10 System . out . println ( " The perimeter of the rectangle is : " , perimeter );
11 }
12 }

Question 13 SumIntegers.java
Write a JAVA program called SumIntegers.java that requests from the user to input a whole
number composed of three digits. Such an input shall be stored in a String called numStr.
The program then will need to compute the average of the three digits constituting this
number and print the result on the screen.

Page 3
Question 14 StringMix.java
Write a JAVA program called StringMix.java which reads from the user two strings S1 and
S2, abd then produces a new String S3 which consists of S1 and S2 mixed in such a way
that S2 is inserted in the middle of S1.

Question 15
Write a program that calculates the energy needed to heat water from a given temperature
to a final temperature. Your program should prompt the user to enter the amount of
water in kilograms and the initial and final temperatures of the water. The formula to
compute the energy is Q = M ∗ (targetT emperature–initialT emperature) ∗ 4184 where
M is the weight of water in kilograms, temperatures are in degrees Celsius, and energy Q
is measured in joules.

Best wishes

Page 4

You might also like