7
CORE JAVA – EXERCISES 1. What's wrong with the following code snippet: 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..... }

Java - Core Java Exercises (2)

Embed Size (px)

Citation preview

Page 1: Java - Core Java Exercises (2)

CORE JAVA – EXERCISES

1. What's wrong with the following code snippet:

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

Page 2: Java - Core Java Exercises (2)

public class EmployeeDemo { public static void main( String[] args ) { Employee.oEmployeeCount = 11; Employee mWorkEmployee = new Employee(); System.out.println( mWorkEmployee.oEmployeeCount ); mWorkEmployee.oEmployeeCount = 27; Employee mTempEmployee = new Employee(); System.out.println( mTempEmployee.oEmployeeCount ); } } 4. Consider the following code: class Entity { private int oEntityId = 0; protected void setEntityId( int aEntityId ) { oEntityId = aEntityId; } protected int getEntityId() { return oEntityId; } } public class ArgumentPassingTest { public static void main( String[] args ) { Entity mWorkEntity = new Entity(); int mWorkEntityCount = 0; mWorkEntity.setEntityId( 10 ); System.out.println( ); //Before function call.. System.out.println( "Before calling modify method: " ); System.out.println( "Entity ID: " + mWorkEntity.getEntityId() ); System.out.println( "Entity Count: " + mWorkEntityCount ); // The Function Call modifyEntityAndCount( mWorkEntity, mWorkEntityCount ); System.out.println( ); //After function call..

Page 3: Java - Core Java Exercises (2)

System.out.println( "After calling modify method: " ); System.out.println( "Entity ID: " + mWorkEntity.getEntityId() ); System.out.println( "Entity Count: " + mWorkEntityCount ); } private static void modifyEntityAndCount( Entity aWorkEntity, int aWorkEntityCount ) { aWorkEntity.setEntityId( 20 ); aWorkEntityCount = 1; System.out.println( ); //During function call.. System.out.println( "In modify method, after modification: " ); System.out.println( "Entity ID: " + aWorkEntity.getEntityId() ); System.out.println( "Entity Count: " + aWorkEntityCount ); } }

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.

public class MutualFund { int oMutualFundId = 0; MutualFund( int aMutualFundId ) { oMutualFundId = aMutualFundId; } public void setMutualFundId( int aMutualFundId ) { ………… // method body ………… } // other methods ………… ………… }

Page 4: Java - Core Java Exercises (2)

public class ProcessMutualFund { void createMutualFund( ) { MutualFund mWorkMutualFund = new MutualFund(); mWorkMutualFund.setMutualFundId(10); ………… // rest of method ………… } } What is the major error in the code? Justify your answer.

6. The source file ProcessEntity.java

is present in the directory C:\development\myproject package myproject; public class ProcessEntity { public static void main( String[] args ) { ……… // method body ……… } } After compiling the file, we run the following commands to run the program:

C:\> cd \development\myproject (Go to the directory C:\development\myproject)

C:\> java ProcessEntity 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?

Page 5: Java - Core Java Exercises (2)

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" );

}

}

class EntityReport extends Report

{

void generateReport( )

{

System.out.println( "Derived class GenerateReport" );

}

}

public class ReportOutput

{

public static void main( String[] args )

{

Report r1 = new Report( );

Report r2 = new EntityReport( );

EntityReport r3 = new EntityReport( );

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.

Page 6: Java - Core Java Exercises (2)

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 ………

}

}

class AdminStatement extends Statement

{

// field declarations ………

public int setStatementId( )

{

// method body ………

}

}

What is wrong with the code above?

9. Consider the code below (all classes are in the same package): class Message

{

String oMessage = "This is the base class message";

public void displayMessage( )

{

System.out.println( oMessage );

}

}

Page 7: Java - Core Java Exercises (2)

class SpecificMessage extends Message

{

String oSpecificMessage = "This is the specific message";

public void displayMessage( int aMessage )

{

System.out.println( aMessage );

}

}

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?