Debugging

Practice Questions

Computer Science › Debugging

Page 1 of 4
10 of 34
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

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?

3

What is the error in the following code?

int val1 = -14,val2 = 4;

final int val3 = 9;

double val4 = 4.1;

double val5 = 3.1;

val1 = val2 * val3;

val3 = val1 * 12;

val5 = val1 - val3;

val4 = val2 + val5;

4

Consider the following code:

public static class Rectangle {

private double width, height;

public Rectangle(double w,double h) {

width = w;

height = h;

}

``

public double getArea() {

return width * height;

}

``

public double getPerimeter() {

return 2 * width + 2 * height;

}

}

``

public static class Square extends Rectangle {

public Square(double s) {

super(s,s);

}

}

public static void main(String[] args) {

Rectangle[] rects = new Rectangle[6];

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

if(i % 2 == 0) {

rects[i] = new Rectangle(i+10,i + 20);

} else {

rects[i] = new Square(i+20);

}

}

Square s = rects[1];

}

What is the error in the code above?

5

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. }
6

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. }
7

Consider the following code:

public static class Rectangle {

private double width, height;

public Rectangle(double w,double h) {

width = w;

height = h;

}

``

public double getArea() {

return width * height;

}

``

public double getPerimeter() {

return 2 * width + 2 * height;

}

}

``

public static class Square extends Rectangle {

public Square(double s) {

super(s,s);

}

}

public static void main(String[] args) {

Rectangle[] rects = new Rectangle[6];

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

if(i % 2 == 0) {

rects[i] = new Rectangle(i+10,i + 20);

} else {

rects[i] = new Square(i+20);

}

}

Square s = rects[1];

}

What is the error in the code above?

8

What is the error in the following code?

int val1 = -14,val2 = 4;

final int val3 = 9;

double val4 = 4.1;

double val5 = 3.1;

val1 = val2 * val3;

val3 = val1 * 12;

val5 = val1 - val3;

val4 = val2 + val5;

9

Given:

const int x = 10;

Which of the following will compile?

10

public interface ServerInstance {

byte[] readBytes();

boolean writeBytes(byte[]b);

boolean wake();

boolean status();

void sleep();

}

``

public class MyHost implements ServerInstance {

boolean running = false;

public boolean wake() {

// Other logic code here...

return running;

}

public boolean status() {

// Other logic code here...

return running;

}

public byte[] readBytes() {

byte[] buffer = null;

// Other logic code here...

return buffer;

}

public void sleep() {

// Other logic code here...

running = false;

}

public byte[] writeBytes(byte[] b) {

// Other logic code here...

return b;

}

// Other methods...

}

What is the error in the code above?

Page 1 of 4
Return to subject