Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

DEPARTMENT OF COMPUTER SCIENCES

SECOND SEMESTER 2021/2022 CSC308 TUTORIAL SHEET I


Exercises
1. What is hardware? What is software?
2. What is the relationship between a high-level language and machine language?
3. What is Java byte code?
4. What is white space? How does it affect program execution? How does it affect program readability?
5. Which of the following are not valid Java identifiers? Why?
a. RESULT
b. result
c. 12345
d. x12345y
e. black&white
f. answer_7
6. What do we mean by the syntax and semantics of a programming language?
7. Give examples of the two types of Java comments and explain the differences between them.
8. Which of the following are not valid Java identifiers? Why?
a. Factorial
b. anExtremelyLongIdentifierIfYouAskMe
c. 2ndLevel
d. level2
e. MAX_SIZE
f. highest$
g. hook&ladder
9. Categorize each of the following situations as a compile-time error, run-time error, or logical error:
a. multiplying two numbers when you meant to add them
b. dividing by zero
c. forgetting a semicolon at the end of a programming statement
d. spelling a word wrong in the output
e. producing inaccurate results
f. typing a { when you should have typed (
10. What is primitive data? How are primitive data types different from objects?
11. What is a string literal?
12. What is the difference between the print and println methods?
13. What is a parameter?
14. What is an escape sequence? Give some examples.
15. What is a variable declaration?
16. How many values can be stored in an integer variable at one time?
17. What are the four integer data types in Java? How are they different?
18. What is a character set?
19. What is operator precedence?
20. What is the result of 19%5 when evaluated in a Java expression? Explain.
21. What is the result of 13/4 when evaluated in a Java expression? Explain.
22. If an integer variable diameter currently holds the value 5, what is its value after the following statement is executed? Explain.
diameter = diameter * 4;
23. If an integer variable weight currently holds the value 100, what is its value after the following statement is executed? Explain.
weight -= 17;
24. Why are widening conversions safer than narrowing conversions?
25. What output is produced by the following code fragment? Explain
System.out.print ("Here we go!");
System.out.println ("12345");
System.out.print ("Test this if you are not sure.");
System.out.print ("Another.");
System.out.println ();
System.out.println ("All done.");
26. What output is produced by the following statement? Explain.
System.out.println ("He thrusts his fists\n\tagainst" + " the post\nand still insists\n\the sees
the \"ghost\"");
27. What value is contained in the integer variable size after the following statements are executed?
size = 18;
size = size + 12;
size = size * 2;
size = size / 4;
28. What value is contained in the floating point variable depth after the following statements are executed?
depth = 2.4;
depth = 20 – depth * 4;
depth = depth / 5;
29. What value is contained in the integer variable length after the following statements are executed?
length = 5;
length *= 2;
length *= length;
length /= 100;
PROGRAMING
P1 Enter, compile, and run the following application:
public class Test
{
public static void main (String[] args)
{
System.out.println ("An Emergency Broadcast");
}
}

P2. Introduce the following errors, one at a time, to the program from the programming project 1.1. Record any error messages that
the compiler produces. Fix the previous error each time before you introduce a new one. If no error messages are produced, explain
why. Try to predict what will happen before you make each change.
a. change Test to test
b. change Emergency to emergency
c. remove the first quotation mark in the string
d. remove the la st quotation mark in the string
e. change main to man
f. change println to bogus
g. remove the semicolon at the end of the println statement
h. remove the last brace in the program
P3. Write an application that prints, on separate lines, your name, your birthday, your hobbies, your favorite book, and your favorite
movie. Label each piece of information in the output.
P4. Write an application that prints the phrase Knowledge is Power:
a. on one line
b. on three lines, one word per line, with the words centered relative to each other
c. inside a box made up of the characters = and |
P5. Write an application that prints a list of four or five Web sites that you enjoy. Print both the site name and the URL.
P6. Write an application that prints the first few verses of a song (your choice). Label the chorus.
P7. Write an application that prints the following diamond shape.
Don’t print any unneeded characters. (That is, don’t make any character string longer than it has to be.)
*
***
*****
*******
*********
*******
*****
***
*

P8. Write an application that prompts for and reads an integer representing the length of a square’s side, then prints the square’s
perimeter and area.

You might also like