Java - Core Java Exercises
Java - Core Java Exercises
if (i = 1)
{
// do something
}
2. What is wrong with the following code? Will it give a compile-time or run-time error?
Why?
class Entity
{
private int oEntityId = 0 ;
protected void setEntityId(int aEntityId)
{
………
}
protected int getEntityId()
{
………
}
}
class EntityTest
{
public static void main(String[] args)
{
………
Entity mWorkEntity = null;
mWorkEntity.setEntityId(10);
System.out.println(mWorkEntity.getEntityId());
…………
return;
}
}
3. Give the output of the println statements in the main( ) function below. For each output
provide an explanation for the output.
class Employee
{
static int oEmployeeCount = 0;
// Member functions.....
}
public class EmployeeDemo
{
public static void main( String[] args )
{
Employee.oEmployeeCount = 11;
System.out.println( mWorkEmployee.oEmployeeCount );
mWorkEmployee.oEmployeeCount = 27;
System.out.println( mTempEmployee.oEmployeeCount );
}
}
class Entity
{
private int oEntityId = 0;
mWorkEntity.setEntityId( 10 );
The basic intention of the code is to attempt to modify the “mWorkEntity” object and the value
of the “mWorkEntityCount” variable by passing them to a function.
i) Does the code succeed in modifying both of them through the function call?
ii) What does it succeed in modifying, and how? Explain the process.
iii) What does it not succeed in modifying, and why? Explain the process.
iv) From all this, does it appear that Java handles objects and primitive variables
differently, from an argument passing point of view?
5. In the following code, both MutualFund and ProcessMutualFund are in the same
package.
// other methods
…………
…………
}
public class ProcessMutualFund
{
void createMutualFund( )
{
MutualFund mWorkMutualFund = new MutualFund();
mWorkMutualFund.setMutualFundId(10);
………… // rest of method
…………
}
}
ProcessEntity.java
package myproject;
After compiling the file, we run the following commands to run the program:
C:\> cd \development\myproject
(Go to the directory C:\development\myproject)
i) Will the program run using these commands? Explain the process to substantiate
your answer. Also, provide the correct steps to be followed in order to run the
program if your answer is “No”.
ii) What should be the directory entry in the CLASSPATH variable?
7. In the following code, the classes Report and EntityReport are present in the same
package.
class Report
{
void generateReport( )
{
System.out.println( "Base class GenerateReport" );
}
}
r1.generateReport( );
r2.generateReport( );
r3.generateReport( );
}
}
What will be the output of the three highlighted lines at the bottom of the main( ) function? Also
give reasons for each output.
8. In the following code, the classes Statement and AdminStatement are present in the same
package.
class Statement
{
// field declarations ………
public final int setStatementId( )
{
// method body ………
}
}
9. Consider the code below (all classes are in the same package):
class Message
{
String oMessage = "This is the base class message";
class PolymorphismDemo2
{
public static void main( String[] args )
{
Message mWorkMessage = new SpecificMessage( );
mWorkMessage.displayMessage( );
}
}
If the above program is executed, which of the two versions of displayMessage( ) (in the base
class and the derived class) will be invoked by the highlighted code? Why?
Is this normal coding practice or is there something wrong about the code?