Question

Something is wrong with each of the following pieces of code. Use A or B to label whether the error is from: A. Compiler ErrorB. Logical or Runtime Error Use minor edits to correct the code. int x System.out.println(value of x is:x) if (x820) System.out.println(Your number was even!) System.out.println(Your number is odd!) else double avg numl+num2+num3/3.0 if (x > y) f double max-x; else max-y: 5. double answer Math.sqrt (/25;
0 0
Add a comment Improve this question Transcribed image text
Answer #1

A. Compiler Error B. Logical or Runtime Error Use minor edits to correct the code.
1. int x;
System.out.println("vellue of x is: " +x); //Compiler Error
Explanation:
public class MyClass {
  
public static void main(String args[]) {

int x;

System.out.println("vellue of x is: " +x);
  
}

}
Output:
Error variable x might not have been initialized

2.
if (x%2==0)
x--;
System.out.println("Your number was even!");
else // Logical or Runtime Error Use minor edits to correct the code.
System.out.println("Your number is odd!"); Explanation:
Explanation:
Program:
public class MyClass {
public static void main(String args[]) {
int x;
if(x%2==0)
x--;
System.out.println("Your number was even!");
else
System.out.println("Your number is odd!");
}
}
Output:Error 'else' without 'if'

3.
double avg = numl+num2+num3/3.0; //Compiler Error
Explanation:
Program:
public class MyClass {
public static void main(String args[]) {
double avg = numl+num2+num3/3.0;
}
}
Output error: cannot find symbol
double avg = num1+num2+num3/3.0;
^
symbol: variable num1
location: class MyClass
/MyClass.java:3: error: cannot find symbol
double avg = num1+num2+num3/3.0;
^
symbol: variable num2
location: class MyClass
/MyClass.java:3: error: cannot find symbol
double avg = num1+num2+num3/3.0;
^
symbol: variable num3
location: class MyClass
3 errors

4.
if (x > y) { //Compiler Error
double max=x;
}
else {
max =y;
}
Explanation:
Program:
public class MyClass {
public static void main(String args[]) {
if (x > y) {
double max=x;
}
else {
max =y;
}
}
}
Output: Error
/MyClass.java:3: error: cannot find symbol
if (x > y) {
^
symbol: variable x
location: class MyClass
/MyClass.java:3: error: cannot find symbol
if (x > y) {
^
symbol: variable y
location: class MyClass
/MyClass.java:4: error: cannot find symbol
double max=x;
^
symbol: variable x
location: class MyClass
/MyClass.java:7: error: cannot find symbol
max =y;
^
symbol: variable max
location: class MyClass
/MyClass.java:7: error: cannot find symbol
max =y;
^
symbol: variable y
location: class MyClass
5 errors

5. double answer = Math.sqrt()/25; //Logical or Runtime Error Use minor edits to correct the code.

Explanation:
Program:
public class MyClass {
public static void main(String args[]) {
double answer = Math.sqrt()/25;
}
}
Output: Error
/MyClass.java:3: error: method sqrt in class Math cannot be applied to given types;
double answer = Math.sqrt()/25;
^
required: double
found: no arguments
reason: actual and formal argument lists differ in length
1 error

Add a comment
Know the answer?
Add Answer to:
Something is wrong with each of the following pieces of code. Use A or B to...
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
  • R3.3 Find the errors in each of the following if statements. a. if x 〉 e...

    R3.3 Find the errors in each of the following if statements. a. if x 〉 e then System.out.print(x); b, if (1 + x > Math . pow(x, Math.sqrt(2)) { y = y + x; c.if(x=1) { y++; } d. x = in·nextInt(); if (in.hasNextInt()) sum = sum + x; else System.out.println("Bad input for x"); e. String letterGrade = "F"; if (grade 〉= 90) { letterGrade _ "A"; } if (grade 〉= 80) { letterGrade- "B"; } if (grade 〉= 70)...

  • Step 4: Add code that discards any extra entries at the propmt that asks if you...

    Step 4: Add code that discards any extra entries at the propmt that asks if you want to enter another score. Notes from professor: For Step 4, add a loop that will validate the response for the prompt question:       "Enter another test score? (y/n): " This loop must only accept the single letters of ‘y’ or ‘n’ (upper case is okay). I suggest that you put this loop inside the loop that already determines if the program should collect...

  • Java code is calculating the bill wrong and not displaying currency at the end: I've highlighted...

    Java code is calculating the bill wrong and not displaying currency at the end: I've highlighted the error in the cosole Southwest Power and Light Billing Statement Please enter your name > J, P Customer name:J, P Meter reading date > 2/15/16 Meter reading date:2/15/16 Electricity used: > 225 The basic rate is:22.5 The totalBill is 225.0 Calculate another bill? (Y/N)?: The correct answer should be baseline charge: $22.50 and total bill: $22.50 The Charges are based on these: 0kw...

  • What are the errors in the following code? My professor gave us this prewritten code and...

    What are the errors in the following code? My professor gave us this prewritten code and asked us to find the errors in it so that it can run properly #include <cstdlib> #include <ctime> #include <iostream> using namespace std; // input: integer // output: none // adds five to the given parameter void addFive( int x ) { x += 5; } // input: none // output: a random number int generateRandomNumber() { srand( time(0) ); return rand() % 100;...

  • 31. The following code segment is syntactically correct:              int number{20};          cout << number <<...

    31. The following code segment is syntactically correct:              int number{20};          cout << number << setbase(16) << " " << number                               << setbase(10) << " " << number                                 << showpos << " " << number << endl;                 T__   F__       32. The following statement wants to determine if ‘count’ is outside the     range of 0 through 100:             if (count < 0 && count > 100)                                             T__   F__ 33. There is...

  • Three of these function overloads are considered identical by the compiler and would conflict with each...

    Three of these function overloads are considered identical by the compiler and would conflict with each other. The other would be considered different- choose the one that is considered different from others. Int foo ( int x, double y ) ; Int foo ( double x, int y ) ; Void foo ( int x, double y ) ; Int foo ( int day, double temp ) ; Consider the following incomplete code: Int f ( int number ) }...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • Eclipse Java Oxygen Question 11 pts A compile-time error occurs when Group of answer choices c....

    Eclipse Java Oxygen Question 11 pts A compile-time error occurs when Group of answer choices c. there’s a syntax error in a Java statement a. the Java compiler can’t be located b. bytecodes can’t be interpreted properly c. there’s a syntax error in a Java statement Flag this Question Question 21 pts An error that lets the application run but produces the wrong results is known as a Group of answer choices d. syntax error c. logic error a. runtime...

  • c++ Some pieces of the following code are missing. Fill in the blanks of the following...

    c++ Some pieces of the following code are missing. Fill in the blanks of the following program. Each answer must fit on a single line. include <iostream> #include <cstdib #include <ctimes using namespace std; randoml_event return 2 (rand() 5 random2 intre + rand 90 return ret: random1_1000X rand) intret 100 return ret: fint 2) return nt; return n2 is oder Is_odot if (n%2 0) return return true; int sum3 (int n, int n, int n3 X int sum= n1 +...

  • With a Scanner object created as follows, what method do you use to read an int...

    With a Scanner object created as follows, what method do you use to read an int value? Scanner input = new Scanner(System.in); A. input.int(); B. input.nextInt(); C. input.integer(); D. input.nextInteger(); What is the output of the following code? x = 0; if (x > 0) System.out.println("x is greater than 0"); else if (x < 0) System.out.println("x is less than 0"); else System.out.println("x equals 0"); A. x is greater than 0 B. x is less than 0 C. x equals 0...

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