0% found this document useful (0 votes)
10 views2 pages

Lab Sheet 2

The document provides a lab sheet with programming exercises. It includes questions about string methods like length, substring, toUpperCase. It also includes questions about output of code snippets involving string manipulation and arithmetic operations.

Uploaded by

sa9661623
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)
10 views2 pages

Lab Sheet 2

The document provides a lab sheet with programming exercises. It includes questions about string methods like length, substring, toUpperCase. It also includes questions about output of code snippets involving string manipulation and arithmetic operations.

Uploaded by

sa9661623
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/ 2

Lab sheet 2

1. Practice on Self-Test Exercises of the chapter.

2. Write a java program to set a string (firstName) to your first name and another string
(lastName) to your last name. Declare a variable (age) of type integer and assign your
age into that variable. Declare a char variable and initialize it by the first letter of your
name.
Print out all the variables and objects.

3. Write a java program to set a string (Greeting) to the string “Hello there”, and print
out the following:
a. The length of the string.
b. The string in lower case.
c. The string in upper case.
d. The substring from position 2.
e. The substring from position 2 to 7.
f. The comparison result with string Greet=”Hello There”

4. What is the output produced by the following?


String verbPhrase = "is money";
System.out.println("Time" + verbPhrase);

5. What is the output produced by the following?


String test = "abcdefg";
System.out.println(test.length());
System.out.println(test.charAt(1));

6. What is the output produced by the following?


String test = "abcdefg";
System.out.println(test.substring(3));

7. What is the output produced by the following?


System.out.println("abc\ndef");

8. What is the output produced by the following?


System.out.println("abc\\ndef");

9. What is the output produced by the following?


String test = "Hello Tony";
test = test.toUpperCase();
System.out.println(test);
10. What is the output of the following two lines of Java code?
System.out.println("2 + 2 = " + (2 + 2));
System.out.println("2 + 2 = " + 2 + 2);

11. Suppose sam is an object of a class named Person and suppose increaseAge is a
method for the class Person that takes one argument that is an integer. How do you
write an invocation of the method increaseAge using sam as the calling object and
using the argument 10 ?

12. The following code is supposed to output the string in lowercase letters but it
has an error. What is wrong?
String test = "WHY ARE YOU SHOUTING?";
test.toLowerCase();
System.out.println(test);

13. Given the following fragment that purports to convert from degrees Celsius to degrees
Fahrenheit, answer the following questions:
double celsius = 20;
double fahrenheit;
fahrenheit = (9 / 5) * celsius + 32.0;

a. What value is assigned to fahrenheit ?


b. Explain what is actually happening, and what the programmer likely wanted.
c. Rewrite the code as the programmer intended.

You might also like