Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Questions 7,9,11,13 System.exit (o) i 25. To format a floating-point number to a

ID: 3607135 • Letter: Q

Question

Questions 7,9,11,13

System.exit (o) i 25. To format a floating-point number to a specific number of decimal places, 26. To input data from a file, you use the classes Scanner and FileReader; 27. File I/O is a four-step process: () import the necessary classes from the you can use the String method format. to send output to a file, you use the class PrintWriter packages java.util and java.io into the program; (ii) create and associate the appropriate objects with the input/output sources; (ii) use the appropriate methods associated with the objects created in Step ii to input/output the data; and (iv) close the file(s). EXERCISES 1. Mark the following statements as true or false A variable declared using a class is called an object. a. b. In the statement x = console. next!nt() ;, x must be a variable. You generate the newline character by pressing Enter (return) on the c. keyboard d. The methods printf and format are used to format a decimal number to a specific number of decimal places. How does a variable of a primitive type differ from a reference variable? 3. What is an object? 4. What does the operator new do? 5. Suppose that str is a String variable. Write a Java statement that uses the operator new to instantiate the object str and assign the string "J Programming" to str What is garbage collection? system to (immediately) perform garbage collection. ava Write the statement that instructs the Java 6. . Which package contains class String? If a program uses this cla . why it is not t necessary to explicitly import this class using the import statement. it is no

Explanation / Answer

Questions 7,9,11,13

Questions 7:

Which package contains class String?

Answer: java.lang.String;

lang package contains class String.

All classes in the java.lang package are imported by default.

Thus we do not need to import java.lang.*;

Questions 9:

String str = "Going to the amusement park";

The first parameter in substring is for starting index.

The second parameter in substring is for end index exclusive.

a) System.out.println(str.substring(0, 5));

Output: Going

Explanation:

Starting index position is 0 i.e., ‘G’ and ending index position is 4 i.e., ‘g’.

So, from the str it will extract data from 0 index position to 4th index position, i.e., Going

b) System.out.println(str.substring(13, 22);

Output: amusement

Explanation:

Starting index position is 13 i.e., character ‘a’ and ending index position is 21 i.e., ‘t’.

So, from the str it will extract data from 13 index position to 21th index position, i.e., amusement

c) System.out.println(str.toUpperCase());

toUpperCase() method is used to convert each character of the string to capital letter.

Output: GOING TO THE AMUSEMENT PARK

d) System.out.println(str.toLowerCase());

toLowerCase() method is used to convert each character of the string to small letter.

Output: going to the amusement park

e) System.out.println(str.replace('t', '*'));

The first parameter of the method replace is the character to replace.

The first parameter of the method replace is the replace character.

It replaces all the characters ‘t’ with characters ‘*’.

Output: Going *o *he amusemen* park

All the occurrence of ‘t’ is replaced with ‘*’.

Questions 11:

The method regionMatches() checks if the two strings are equal.

This method compares the substring of input string with the substring of specified string.

The above function takes 4 parameters.

First parameter – If the first parameter value is true then ignore case when comparing characters.
Second parameter – The starting offset of the sub-region in this string.
Third parameter – The string argument.
Fourth parameter – The starting offset of the sub-region in the string argument.
Fifth parameter – The number of characters to compare.

String str;

String str1 = "programming";

a) str = "Java programming: from problem analysis to program design";

System.out.println(str.regionMatches(6, str1, 0, str1.length()));

Output: false

Explanation:

6th index position to end of the string str is compared with second sting str1’s 0th index position to length of the str1.

b) System.out.println(str.regionMatches(true, 31, "Analysis", 0, 8));

Output: true

Explanation:

First parameter is true means ignore case when comparing characters.

31th index position of str is compared with the string “Analysis” starting from 0 to 8 characters of the specified string “Analysis”.

Questions 13:

import java.util.*;

// Class Demo definition

public class Demo

{

      // main method definition

      public static void main(String[] args)

      {

            // Scanner class object created to accept data from the user from console

            Scanner sc = new Scanner(System.in);

            System.out.print("Entered a name: ");

            // Accepts data using method nextLine() of scanner class which accepts data including space

            String name = sc.nextLine();

            // Displays the name entered by the user

            System.out.println("Entered name = " + name);

      }// End of main method

}// End of class

Entered a name: Brenda Clinton

Entered name = Brenda Clinton