0% found this document useful (0 votes)
20 views10 pages

Java Formatting and String Quiz

Uploaded by

dongshengche2013
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views10 pages

Java Formatting and String Quiz

Uploaded by

dongshengche2013
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CMSC130 Introduction to Computer Programming I

Quiz: Java Formats and Strings

Name:

Put your answers in the box below


1 2 3 4 5 6 7 8 9 10

11 12 13 14 15 16 17 18 19 20

21 22 23 24 25 26 27 28 29 30

X X

1. Consider the following printf statement to format an integer with %d:


public class F1 {
public static void main(String[] args) {
[Link]("Value=%d", 5);
}
}
What is the exact output

(A) Value=5
(B) Value=%d
(C) Value= 5.0
(D) Compilation error

2. Consider the following code using %s:


public class F2 {
public static void main(String[] args) {
String lang = "Java";
[Link]("Hello %s", lang);
}
}
What is printed

1
(A) Hello Java
(B) Hello %s
(C) Hello
(D) Java Hello

3. Right alignment with field width 3 for an integer:

public class F3 {
public static void main(String[] args) {
[Link]("%3d", 7);
}
}

Using underscores to denote spaces, which is correct

(A) 7
(B) 7
(C) 007
(D) 7.0

4. Left alignment with field width 3 for an integer:

public class F4 {
public static void main(String[] args) {
[Link]("%-3d", 7);
}
}

Using underscores to denote spaces, which is correct

(A) 7
(B) 7
(C) 007
(D) 7

5. Printing a floating-point number with two digits after the decimal using %.2f:

public class F5 {
public static void main(String[] args) {
[Link]("%.2f", 3.14159);
}
}

What is the output

2
(A) 3.14
(B) 3.142
(C) 3.1
(D) 3

6. Using multiple format specifiers in one printf:

public class F6 {
public static void main(String[] args) {
[Link]("Name=%s Age=%d", "Ann", 20);
}
}

What is printed

(A) Name=Ann Age=20


(B) Name=%s Age=%d
(C) Ann 20
(D) Compilation error

7. Zero-padding an integer to width 5:

public class F8 {
public static void main(String[] args) {
[Link]("%05d", 7);
}
}

What is the output

(A) 00007
(B) 7
(C) 007
(D) 7

8. Truncating a string with precision %.3s:

public class F9 {
public static void main(String[] args) {
[Link]("%.3s", "Hello");
}
}

What is printed

3
(A) Hel
(B) Hello
(C) He
(D) ello

9. Combining width and precision for a floating number:

public class F10 {


public static void main(String[] args) {
[Link]("%6.2f", 3.14159);
}
}

Using underscores to denote spaces, which is correct

(A) 3.14
(B) 3.14
(C) 3.1
(D) 3

10. Evaluate the following call to find string length:

public class S1 {
public static void main(String[] args) {
String s = "Hello World";
[Link]([Link]());
}
}

What is printed

(A) 11
(B) 10
(C) 12
(D) 9

11. Accessing a character at index 1:

public class S2 {
public static void main(String[] args) {
String s = "Hello";
[Link]([Link](1));
}
}

4
What is printed

(A) e
(B) H
(C) l
(D) o

12. Extracting a substring from index 1 to 4 (exclusive):


public class S3 {
public static void main(String[] args) {
String s = "Hello";
[Link]([Link](1,4));
}
}
What is printed

(A) ell
(B) ello
(C) Hel
(D) lo

13. Finding the index of a substring:


public class S4 {
public static void main(String[] args) {
String s = "Welcome";
[Link]([Link]("me"));
}
}
What does this print

(A) 6
(B) 5
(C) 7
(D) -1

14. Checking prefix with startsWith:


public class S5 {
public static void main(String[] args) {
String s = "Welcome";
[Link]([Link]("Wel"));

5
}
}
What does this print

(A) true
(B) false
(C) 0
(D) Compilation error

15. Checking suffix with endsWith:


public class S6 {
public static void main(String[] args) {
String s = "Welcome";
[Link]([Link]("me"));
}
}
What does this print

(A) true
(B) false
(C) me
(D) Compilation error

16. Which expression checks string content equality:


(A) [Link](s2)
(B) s1 == s2
(C) equals(s1,s2)
(D) s1 = s2

17. Comparing strings with case sensitivity options:


public class S8 {
public static void main(String[] args) {
String a = "Java", b = "java";
[Link]([Link](b) + " " + [Link](b));
}
}
What is printed

(A) true false


(B) false true

6
(C) true true
(D) false false

18. Converting to upper-case:

public class S9 {
public static void main(String[] args) {
[Link]("Hello".toUpperCase());
}
}

What is printed

(A) HELLO
(B) hello
(C) Hello
(D) Compilation error

19. Converting to lower-case:

public class S10 {


public static void main(String[] args) {
[Link]("WORLD".toLowerCase());
}
}

What is printed

(A) world
(B) WORLD
(C) World
(D) wORLD

20. Comparing two identical strings using compareTo:

public class S11 {


public static void main(String[] args) {
[Link]("Apple".compareTo("Apple"));
}
}

What does this print

(A) 0
(B) positive number

7
(C) negative number
(D) Exception

21. Lexicographic order using compareTo:

public class S12 {


public static void main(String[] args) {
[Link]("Apple".compareTo("Banana"));
}
}

What does this print

(A) Negative number


(B) Positive number
(C) 0
(D) Compilation error

22. Replacing characters using replace(char,char):

public class S13 {


public static void main(String[] args) {
[Link]("Java".replace(’a’,’x’));
}
}

What is printed

(A) Jxvx
(B) Java
(C) Jxva
(D) Compilation error

23. Replacing substrings using replace(String,String):

public class S14 {


public static void main(String[] args) {
[Link]("Java".replace("av","++"));
}
}

What is printed

(A) J++a
(B) Java

8
(C) Jav++
(D) Compilation error

24. Splitting a CSV string by comma:


public class S15 {
public static void main(String[] args) {
String s = "A,B,C";
String[] parts = [Link](",");
[Link]([Link]);
}
}
What is printed

(A) 3
(B) 2
(C) 1
(D) 4

25. Substring from a start index to the end:


public class S16 {
public static void main(String[] args) {
String s = "Welcome";
[Link]([Link](3));
}
}
What is printed

(A) come
(B) Wel
(C) Welcome
(D) elc

26. Index not found case for indexOf:


public class S17 {
public static void main(String[] args) {
[Link]("Welcome".indexOf("xyz"));
}
}
What is printed

9
(A) -1
(B) 0
(C) Compilation error
(D) Exception

27. For s = "Java", what are [Link]() and the last valid index:
(A) 4 and 3
(B) 4 and 4
(C) 3 and 2
(D) 5 and 4

28. Accessing a character using a computed index:

public class S20 {


public static void main(String[] args) {
String s = "Java";
[Link]([Link]([Link]()-2));
}
}

What is printed

(A) v
(B) a
(C) J
(D) Compilation error

10

Common questions

Powered by AI

Java's substring method returns the portion of the string specified by the start and end indices. For example, 'Hello'.substring(1,4) extracts characters from index 1 to 3, resulting in 'ell' as it includes indices 1, 2, and 3 but excludes index 4 . When only a start index is given, such as 'Welcome'.substring(3), it extracts all characters from the start index to the end of the string, resulting in 'come' .

Java's replace method allows for the substitution of characters or substrings within a string. The method replace(char, char) substitutes all occurrences of a character with another, such as 'Java'.replace('a', 'x') producing 'Jxvx'. The replace(String, String) method replaces substrings, where 'Java'.replace('av', '++') results in 'J++a' . These methods are effective for transforming strings for various needs, such as formatting output or sanitizing input.

Using toUpperCase converts all characters in a string to uppercase, while toLowerCase converts all characters to lowercase. For example, 'Hello'.toUpperCase() results in 'HELLO' and 'WORLD'.toLowerCase() results in 'world' . These methods are useful for formatting text data consistently, regardless of the input case, thereby ensuring a uniform case for operations like storage or display.

Java manages space in formatted output using printf with field width and alignment specifiers. Right alignment by default is handled by indicating field width, hence System.out.printf("%3d", 7) outputs ' 7' by adding spaces at the start. Left alignment uses a negative width specifier, as in System.out.printf("%-3d", 7) which outputs '7 ' by adding spaces after the number . These features enable precise control over the presentation of data for user interfaces or printed reports.

To correctly extract and interpret string length and character indices in Java, it is crucial to understand that string indices start at 0, so the maximum valid index is one less than the string length. For example, given the string "Java", the length is 4 and the indices range from 0 to 3 . Accessing characters requires careful handling to avoid IndexOutOfBoundsException, as accessing an out-of-range index will lead to runtime errors. Interpretations of length via s.length() return the full count of characters, while accessing specific indices employs charAt(). This knowledge is essential for operations like iterations, substring extraction, and character manipulation.

The compareTo method in Java compares two strings lexicographically and returns an integer. If the first string is less than the second string, it returns a negative value; if it is greater, it returns a positive value; and if they are equal, it returns zero. For example, 'Apple'.compareTo('Banana') would return a negative number because 'Apple' is lexicographically less than 'Banana', whereas 'Apple'.compareTo('Apple') returns 0 as the strings are equal .

The split method in Java divides a string into an array based on the provided delimiter. For instance, "A,B,C".split(",") results in an array with three elements: ["A", "B", "C"], and parts.length would return 3 . This method is efficient for parsing delimited text data, such as CSV files, into manageable parts for processing.

Java handles string comparison in a case-sensitive manner by default, using the equals method. This method will return false if there is any difference in case, e.g., 'Java'.equals('java') results in false. However, the equalsIgnoreCase method can be used for a case-insensitive comparison, where 'Java'.equalsIgnoreCase('java') results in true . This feature is useful for situations where string comparison needs to ignore case differences.

Using different format specifiers in Java's printf method allows for specific formatting of output. For instance, %d is used to format integers, as shown in the example where System.out.printf("Value=%d", 5); outputs 'Value=5' . When formatting strings, the %s specifier is used — System.out.printf("Hello %s", "Java"); results in 'Hello Java' . Zero-padding an integer can be done using %05d, resulting in '00007' if the input is 7. Field width specifiers, such as %3d for right alignment, pad the integer with spaces to align it, and %-3d for left alignment ensures spaces are added after the digit .

The indexOf method returns -1 when the specified substring or character is not found in the string. For instance, "Welcome".indexOf("xyz") results in -1 as the substring "xyz" does not exist within "Welcome" . This behavior helps in determining the presence and location of substrings, and a return value of -1 is a common way to indicate non-existence within string search operations.

You might also like