Computer Science › Compile Time Errors
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?
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?
Given:
const int x = 10;
Which of the following will compile?
public static void main(String[] args) {
int[] x = {3,4,4,5,17,4,3,1};
int[] y = remove(x,4);
for(int i = 0; i < y.length; i++) {
System.out.print(y[i] + " ");
}
}
``
public static boolean remove(int[] arr, int val) {
boolean found = false;
int i;
for(i = 0; i < arr.length && !found; i++) {
if(arr[i] == val) {
found = true;
}
}
if(found) {
for(int j = i; j < arr.length;j++) {
arr[j - 1] = arr[j];
}
arr[arr.length - 1] = 0;
}
return found;
}
What is the error in the code above?
public static void foo() {
int x = 10; y = 21, z = 30;
int[] arr = null;
for(int i = 0; i < y; i+= 4) {
arr = new int[i / 5];
}
for(int i = 0; i < x; i++) {
arr[i] = z / i;
}
for(int i = 0; i < z; i++) {
arr[i] = z * i;
}
}
Which of the following lines of code will cause a compile-time
error?
class Base{
protected:
void method();
};
class Derived : public Base{
};
int main(){
Base b;
b.method(); //Line A
Derived d;
d.method(); //Line B
}
Which of the following is true?
class Base{};
class Derived : public Base{
public:
void method(){ cout<< "method1
"; }
};
class Derived2 : public Base{
public:
void method() { cout<< "method2
"; }
};
int main(){
Base* bp = new Derived();
Derived2* d2p = bp;
d2p -> method();
}
What is the result of compiling and running the program in C++?