0% found this document useful (0 votes)
14 views

Cmps201 Lab 1

(1) The document is a lab assignment for a Computer Science course titled CMPS201 from the American University of Beirut. It contains 6 questions to test students' knowledge of Java programming concepts like variables, operators, and data types. (2) Students are asked to determine the values displayed when printing various Java expressions, identify true/false statements about Java, write code to print text with special characters, and identify the output of a sample Java program. (3) The deadline to complete the assignment is Friday midnight.

Uploaded by

haladaher5555
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Cmps201 Lab 1

(1) The document is a lab assignment for a Computer Science course titled CMPS201 from the American University of Beirut. It contains 6 questions to test students' knowledge of Java programming concepts like variables, operators, and data types. (2) Students are asked to determine the values displayed when printing various Java expressions, identify true/false statements about Java, write code to print text with special characters, and identify the output of a sample Java program. (3) The deadline to complete the assignment is Friday midnight.

Uploaded by

haladaher5555
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

American University Of Beirut

Department of Computer Science


CMPS201 - LAB 1

Deadline: Friday Midnight Fall 2023 Date: September 4, 2023

Attempt All questions

1. State whether each of the following is true or false. If false, explain why.
(a) Comments cause the computer to print the text after the // on the screen when the
program executes.
(b) All variables must be given a type when they’re declared.
(c) Java considers the variables number and NuMbEr to be identical.
(d) The println() function is used to display text on the screen in Java.
(e) The expression 5 + 3 * 2 evaluates to 16 in Java.
(f) An identifier in Java can start with a number, e.g., 3count.
(g) Syntax errors occur when the program runs but produces incorrect results.
(h) To include a double quote (") character within a string in Java, you can directly use
it without any special escape sequence.

2. Determine the values displayed when printing the following expressions in println state-
ments. Write your answers in the second column.

Expression Answer
(10 + 5) ∗ 2/5
8%3 + 7/2
15%4 − (2 + 3)/4
1+5∗5−6∗3
12/0

3. What is the output of the following expression and respond with “Error” for an invalid
expression:

Expression Output
System.out.println(1*3 + "I will
work on CS201");
System.out.println("CMPS 201" + 1 +
1);
System.out.println("CMPS 201" * 3);

4. Implement a Java program to print the following text (with the same layout):
"\n" represents a newline character.
"\t" represents a tab character.
"\\" represents a backslash character.
"\"" represents a double quote character.

5. Determine the values displayed when printing the following expressions in println state-
ments. Write your answers in the second column.

Expression Answer
49/5
10 + 100/10 ∗ 5 − 20.5/5
49.0/5
”CMPS” + 200 + 12 + ”L”
”CMPS” + (200 + 12) + ”L”
200 + 1 + "CMPS"

5.2 + 31/10

6. What is the Output of the following Java Code ?

1 public class Ex pressi onPrin ter {


2 public static void main ( String [] args ) {
3 // Define some integer and float values
4 int numInt = 10;
5 float numFloat = 3.14 f ;
6
7 // Calculate and print expressions
8 System . out . println ( " Expression 1: " +
9 numInt + " + " + numFloat + " = " + ( numInt + numFloat ));
10

11 System . out . println ( " Expression 2: " +


12 numInt + " - " + numFloat + " = " + ( numInt - numFloat ));
13
14 System . out . println ( " Expression 3: " +
15 numInt + " * " + numFloat + " = " + ( numInt * numFloat ));
16

17 System . out . println ( " Expression 4: " +


18 numInt + " / " + numFloat + " = " + ( numInt / numFloat ));
19
20 // Define a text value
21 String text = " Hello , " ;
22
23 // Combine text with numbers and print
24 System . out . println ( " Expression 5: Text + Integer = "
25 + text + numInt );
26
27 System . out . println ( " Expression 6: Text + Float = "
28 + text + numFloat );
29
30 }
31 }

Best wishes

Page 2

You might also like