Identifying & Correcting Errors

Practice Questions

Computer Science › Identifying & Correcting Errors

Questions
5
1

FILE INPUT/OUTPUT

Consider the following C++ code:

1. #include

2. #include

3. using namespace std;

4. int main() {

5. ifstream outputFile;

6. inputFile.open("TestFile.txt");

7. outputFile << "I am writing to a file right now." << endl;

8. outputFile.close();

9. //outputFile << "I'm writting on the file again" << endl;

10. return 0;

11. }

What is wrong with the code?

2

What is wrong with the following code?

  1. int main()
  2. {
  3. bool logic;
  4. double sum=0;
  5. for(j=0;j<3;j++)
  6. {
  7. sum=sum+j;
  8. }
  9. if (sum>10)
  10. {
  11. logic=1;
  12. }
  13. else
  14. {
  15. logic=0;
  16. }
  17. }
  18. }
3

Consider the following code:

int num = 1;

for(int i = 0; i < 10; i++) {

num *= i;

}

The code above is intended to provide the continuous product from num to 10. (That is, it provides the factorial value of 10!.) What is the error in the code?

4

a. public void draw() {
b. int i = 0;
c. while(i < 15){
d. system.out.println(i);
e. i++
f. }
g. }

Which lines of code have errors?

5

Consider the code below:

public class Clock {

private int seconds;

``

public Clock(int s) {

seconds = s;

}

``

public void setTime(int s) {

seconds = s;

}

``

public void setSeconds(int s) {

int hoursMinutes = seconds - seconds % 60;

seconds = hoursMinutes + s;

}

``

public void setMinutes(int min) {

int hours = seconds / 3600;

int currentSeconds = seconds % 60;

seconds = hours + min * 60 + currentSeconds;

}

}

Which of the following issues could be raised regarding the code?

Return to subject