Question

In: Computer Science

[after §3.22 − easy] String Processing : Consider the following code fragment: 1 int a =...

  1. [after §3.22 − easy] String Processing :

Consider the following code fragment:

1 int a = 20;

2 int b;

3 double x = 3.5;

4 String s = "All";

5 char ch;

6

7 x += a;

8 x--;

9 a /= 4 - 1;

10 b = s.length();

11 b += 4;

12 s += "is well";

13 ch = s.charAt(b);

14 System.out.println("a = " + a + ", b = " + b);

15 System.out.println("x = " + x + "\ns = " + s);

16 System.out.println("ch = " + ch);

Trace the above code, using the following format:

Use a separate row for each statement.

Write a Java program that executes the commands in the code fragment.

Solutions

Expert Solution

import java.io.*;

class StringProcessing {
        public static void main (String[] args) {
            int a=20,b;
            double x=3.5;
            String s="All";
            char ch;
            x+=a;             //return x=23.5
            x--;              //return x=22.5
            a/=4-1;           //return a=20/(4-1)=6
            b=s.length();     //return b=3 as length of s is 3
            b+=4;              //return b=b+4=3+4=7;
            s+="is well";      //s="All"+"is well"= "Allis well"
            ch=s.charAt(b);    //ch=s.charAt(7)= e 
                System.out.println("a= "+a+", b= "+b);
                System.out.println("x= "+x+"\ns= "+s);
                System.out.println("ch= "+ch);
        }
}


Related Solutions

For Questions 1-3: consider the following code: public class A { private int number; protected String...
For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {    super();    System.out.println(“B() called”);...
1. What will be the value of numbers[1] after the following code is executed? int[] numbers...
1. What will be the value of numbers[1] after the following code is executed? int[] numbers = {22, 33, 44}; for(int k = 0; k < 3; k++) { numbers[k] = numbers[k] + 5; } } 2. What will be the results of the following code? final int ARRAY_SIZE = 5; double[] x = new double[ARRAY_SIZE]; for(int i = 1; i <= ARRAY_SIZE; i++) { x[i] = 10.0; } 3.What is the value of numbers [3] after the following line...
1) What's the contents of array vals after the following code is executed? int[] vals =...
1) What's the contents of array vals after the following code is executed? int[] vals = {6, 1, 4, 5, 1}; int size = vals.length; for(int i = 0; i < size; i++) vals[i] = vals[i] * i; q) [ 6, 1, 4, 5, 1 ] b)[ 0, 1, 2, 3, 4] c)[0, 1, 8, 15, 4] d)[6, 2, 12, 20, 5] 2) Consider function foo defined below: static int[] foo(int vals[]) { int result[] = new int[vals.length - 1];...
Q1# Consider the following string: String hello = "Hi girls! hope your assignment1 is very easy.";...
Q1# Consider the following string: String hello = "Hi girls! hope your assignment1 is very easy."; a. What is the value displayed by the expression hello.length()? b. What is the value returned by the method call hello.charAt(12)? c. Write an expression that refers to the letter p in the string referred to by hello Q2# Modify the program to use a "while-do" loop instead of "for" loop. int sum = 0; int number = lowerbound; // declare and init loop...
Consider the following class: class Person {         String name;         int age;        ...
Consider the following class: class Person {         String name;         int age;         Person(String name, int age){                this.name = name;                this.age = age;         } } Write a java program with two classes “Teacher” and “Student” that inherit the above class “Person”. Each class has three components: extra variable, constructor, and a method to print the student or the teacher info. The output may look like the following (Hint: you may need to use “super”...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp; } void function(int arr[], int length) {        for (int i = 0; i<length / 2; i++)               swap(arr, i, (length / 2 + i) % length); } If the input to the function was int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 }; function(arr,8); What values would be stored in the array after calling the...
What’s the output of following code fragment: #include<stdio.h> #include<signal.h> void response (int sig_no) { printf("25"); }...
What’s the output of following code fragment: #include<stdio.h> #include<signal.h> void response (int sig_no) { printf("25"); } void response2 (int sig_no) { printf("43"); }     int main() {      int id = getpid();      signal(SIGUSR1, response); signal(SIGKILL, response2); for(int i=0; i<4; i++) { sleep(1); if (i % 3 == 0) { kill(id, SIGUSR1); } else { kill(id, SIGKILL); } } return 0; }
Javascript. Consider the following code fragment, that is supposed to compute the pixel value let c...
Javascript. Consider the following code fragment, that is supposed to compute the pixel value let c = image.getPixel(x, y); const m1 = (c[0] + c[1] + c[2]) / 3; c = image.getPixel(x + 1, y); const m2 = (c[0] + c[1] + c[2]) / 3; image.setPixel(x, y, [m1 - m2, m1 - m2, m1 - m2]); Give three pairs of pixel values (x, y) = [?, ?, ?] and (x+1, y) = [?, ?, ?] in the input image, for...
Consider the following code fragment and answer below 1. Read(fd, user entry, size of (user entry));...
Consider the following code fragment and answer below 1. Read(fd, user entry, size of (user entry)); 2. Comp=memcmp(userEntry, correct password, stream( user Entry)); 3. If (Comp!=0) 4. Return BAD_PASS - identify any 3 problems with code 8 for each defect describe: - A) what is this problem - B) how it can be found (code, review, Static analysis.....) - C) how it can be solved
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n...
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n == 0) return 0; else if (n==1) return 1; else return fib(n-1) + fib (n-2); } For this programming assignment, write and test an ARMv8 program to find Fibonacci (n). You need to write a main function that calls the recursive fib function and passes an argument n. The function fib calls itself (recursively) twice to compute fib(n-1) and fib (n-2). The input to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT