Question
java

a. What is printed by the following code? Explain your answer by showing how you computed the values of pos, s1, s2 and 53. S
0 0
Add a comment Improve this question Transcribed image text
Answer #1

a.

CODE:

COPY (for copy):

//Main Class
class Abc
{   
   //Main Method
public static void main(String []args){
    //Storing string in str variable
String str = "2900 Bedford Ave Brooklyn NY 12230-6843";
/*
str.indexOf(" ",'B') return value -1 cause 'B' is not an integer
    Therefore, pos = str.indexof(" ", -1 + 1) finds space from position 0
    which is 4. So, pos = 4.
*/
int pos = str.indexOf(" ", str.indexOf(" ",'B') + 1);
//s1 will be string from 0 to 4 of str which is "2900"
String s1 = str.substring(0, pos);
//Output will be "pos: 4 s1: 2900"
System.out.println("pos: " + pos + " s1: " + s1);
/*pos = str.indexOf("B", 4 + 1) will find "B" from position 5 which is
5. So, pos = 5.
       */
pos = str.indexOf("B", pos + 1);
/*pos = str.indexOf("B", 5 + 1) will find "B" from position 6 which is
17. So, pos = 17.
       */
pos = str.indexOf("B", pos + 1);
/*s2 will be string from 17 to str.indexOf(" ", 17) will find space from
position 17 which is 25. So, s2 will be "Brooklyn".
*/
String s2 = str.substring(pos, str.indexOf(" ", pos));
//Output will be "pos: 17 s2: Brooklyn"
System.out.println("pos: " + pos + " s2: " + s2);
/*str.lastIndexOf("1") finds the last position of "1" which is 29. s3
will be 29 to end of string
*/
String s3 = str.substring(str.lastIndexOf("1"));
//Output will be "12230-6843"
System.out.println("s3: " + s3);
   }
}

OUTPUT:

b.

CODE:

CODE (for copy):

//Main Class
class Abc
{   
   //Main Method
public static void main(String []args){
    //Storing string s1 and s2 variable
String s1 = "01234";
String s2 = "98765";
//Concatination of s2 and s1 in s3
String s3 = s2 + s1;
//Loop from 0 to length of s2
for(int i = 0; i < s2.length(); i++){
   //Concatination of s2 letter by letter in s1
   s1 = s2.charAt(s2.length() - i - 1) + s1;
}
//Output will be "9876501234"
System.out.println("s1: " + s1);
//Output will be "9876501234"
System.out.println("s3: " + s3);
//s1 and s3 are same as string but variable are not same
if(s1 == s3)
   System.out.println("s1 and s3 are equal");
else
   System.out.println("s1 and s3 are not equal");
   }
}


OUTPUT:

Please comment for any doubt.

Add a comment
Know the answer?
Add Answer to:
java a. What is printed by the following code? Explain your answer by showing how you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. What is output by the following code? String s0 = "Java"; int i1 = s0.indexOf('a');...

    1. What is output by the following code? String s0 = "Java"; int i1 = s0.indexOf('a'); int i2 = s0.indexOf('a', i1); int i3 = s0.indexOf('a', i1 + 1); System.out.print(i1 + " " + i2 + " " + i3); 2. What is output by the following code? String s1 = "Java"; String s2 = ""; for (int i = 0; i < s1.length(); i++) { s2 += s1; } System.out.print(s2);

  • Java 1. Can you explain it how it will be printed step by step? Thanks!! What...

    Java 1. Can you explain it how it will be printed step by step? Thanks!! What is printed by the following? for (int i=1; i<4; i++) { for (char c=’a’; c<=’c’; c++) { if (i%2==0) { i++; System.out.println(i + " " + c); } else { c++; System.out.println(c + " " + i); } } } 2. Is there a compiler error in this code? If so, what is it? If not, what’s printed? Is there a compiler error in...

  • Write Java Programs to perform the following: 1. To create a new String object 2. To...

    Write Java Programs to perform the following: 1. To create a new String object 2. To find the length of the string 3. Concatenation operator 4. To display the content in String array 5. A complete Java Program that incorporates the following String methods taught in class a. toUpperCase()) b. to LowerCase()) C. replace('0','A')) d. s1.length() e. compareTo(s1)) methods taught in class a. toUpperCase()) b. to LowerCase()) C. replace('0','A')) d. s1.length()) e. compareTo(s1)) f. 51.substring(4)) g. s2.substring(2,10)) h. s1.indexOf('g')) i....

  • Write a Java program for all items in Question Use System.out.println() for each resulting string. Let...

    Write a Java program for all items in Question Use System.out.println() for each resulting string. Let s1 be " Welcome " and s2 be " welcome ". Write the code for the following statements: a. Check whether s1 is equal to s2 and assign the result to a Boolean variable isEqual. b. Check whether s1 is equal to s2, ignoring case, and assign the result to a Boolean variable isEqual. c. Compare s1 with s2 and assign the result to...

  • how would I complete this code without calling any built-in java collection framework classes like ArrayList,...

    how would I complete this code without calling any built-in java collection framework classes like ArrayList, LinkedList, etc? import java.util.Iterator; class CallStack<T> implements Iterable<T> { // You'll want some instance variables here public CallStack() { //setup what you need } public void push(T item) { //push an item onto the stack //you may assume the item is not null //O(1) } public T pop() { //pop an item off the stack //if there are no items on the stack, return...

  • 1. What does the following code print to the console? LocalDate date = LocalDate.of(1976, Month.JANUARY, 1);...

    1. What does the following code print to the console? LocalDate date = LocalDate.of(1976, Month.JANUARY, 1); DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM); System.out.println(dtf.format(date)); a. 01/01/1976 b. Jan 1, 1976 c. 1976-01-01 d. 01/01/76 2. What is the value of s2 after the code that follows is executed? String s1 = "118-45-9271"; String s2 = ""; for (int i = 0; i < s1.length(); i++) {     if (s1.charAt(i) != '-') {         s2 += s1.charAt(i);     } } s2 = s2.replace('-', '.'); a....

  • Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 {...

    Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 { public static void whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) { B[i]=A[i]*2; } A=B; } public static void main(String args[]) { int A[] = {10,20,30}; whatHappens(A); System.out.println(A[0]); } } will print 10. explain how it's works. Thanks public class WhatsPrinted3 { public static int[] whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) {...

  • Hello, Can you please error check my javascript? It should do the following: Write a program...

    Hello, Can you please error check my javascript? It should do the following: Write a program that uses a class for storing student data. Build the class using the given information. The data should include the student’s ID number; grades on exams 1, 2, and 3; and average grade. Use appropriate assessor and mutator methods for the grades (new grades should be passed as parameters). Use a mutator method for changing the student ID. Use another method to calculate the...

  • JAVA 1.            Given the following class definition, what are the contents of the fields x and...

    JAVA 1.            Given the following class definition, what are the contents of the fields x and y of the object p ?    class MyPoint {      int x;      int y; public MyPoint (int x, int y){        x = x;        y = y;      } --------------------------------------- MyPoint p = new MyPoint( 100, 88 ); 2.            static and non-static members What would happen if you tried to compile and run the following code ? public class Driver {...

  • For Python 3.7+. TEST THE CODE WITH THE FOLLOWING GLOBAL CODE AFTER YOU'RE DONE: print('\nStart of...

    For Python 3.7+. TEST THE CODE WITH THE FOLLOWING GLOBAL CODE AFTER YOU'RE DONE: print('\nStart of A2 Student class demo ') s1 = Student('David Miller', major = 'Hist',enrolled = 'y', credits = 0, qpoints = 0) s2 = Student('Sonia Fillmore', major = 'Math',enrolled = 'y', credits = 90, qpoints = 315) s3 = Student('A. Einstein', major = 'Phys',enrolled = 'y', credits = 0, qpoints = 0)          s4 = Student('W. A. Mozart', major = 'Mus',enrolled = 'n', credits = 29, qpoints...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT