Computer Science › Debugging
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?
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?
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;
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?
What is wrong with the following code?
What is wrong with the following code?
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?
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;
Given:
const int x = 10;
Which of the following will compile?
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?