In: Computer Science
Identify which of the following statements will generate an error. If there is an error, which phase of compiler construction (if any) will be suitable to detect the following errors and why?
1. A function is defined with the same signature as the previous one.
2. A variable named ‘new’ is defined and initialized two times. Once in main function and second in a “for” loop inside main function.
3. A multi-line comment that starts but not ends.
4. The following assignment expression is made where “var” is of Boolean datatype. var=10;
5. A function named “func1” is defined as follows public abstract protected int func1(float x,int y){…}
6. A class named “demo_class” is defined that implements an interface “X” demo_class Class implements X{…}
7. A while loop is defined that runs infinitely.
8. A string constant that starts with an inverted comma but does not end.
9. The following do- while loop is created without any statement. int x=1; do{ }while(x>10)
10. An interface “Int1” is created that inherits another interface “Int2” and contains two variables as follows interface Int1 inherits Int2 { int var1, var2; }
1) Error
It will generate error due to same signature of
function .To avoid it the try method overloading .
-------------------------------------------------------------------------------------------------------------
2) No Error
Everytime when new operator is called it will assign a
new memory and that object is created .
--------------------------------------------------------------------------------------------------------------
3) Error
In multiline comment the compiler should know that
which part is commented in code otherwise it will skip all the
parts from where the comment begins.
To avoid it use */ at the end of the line where you
want to stop the comment.
---------------------------------------------------------------------------------------------------------------
4)Error
The var is of type Boolean and integer is assigned to
it.So,type mismatch error will be generated .Boolean will contain
only True and False.
------------------------------------------------------------------------------------------------------------------------
5) Error
Abstract methods does not have body.It is just
reference to derived class that when inherits the base class the
derived class should implement the abstract method of base
class.
-------------------------------------------------------------------------------------------------------------------------
6)Error
It will generate a error because of demo_class Class
implements X{…}.
“Class classname implements interfacename {}” is the correct syntax to implement a interface.
so the error would be resolved by
Class demo_class implements X{...}
------------------------------------------------------------------------------------------------------------------------
7)No Error
It will stop execution when the system is haulted or
the memory is full.
--------------------------------------------------------------------------------------------------------------------------------
8)Error
The String is represented inside "" .So if the
otherside " is not there then it will generate error of String
literal is not properly closed by ";
------------------------------------------------------------------------------------------------------------------------------------------
9)No Error
It will move once in the loop as it is do-while loop
even if the condition of while is false.
--------------------------------------------------------------------------------------------------------------------------------------------------
10)Error
Interface are extended or implemented.
For class we use implements keyword to use the
Interface.
For Interface it use extends keyword to use the other
interface.
Class implements interface
Interface extends other interface