Java - Core Java Exercises (2)

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Java - Core Java Exercises (2) as PDF for free.

More details

  • Words: 879
  • Pages: 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.....

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

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) ii) iii) iv)

5.

Does the code succeed in modifying both of them through the function call? What does it succeed in modifying, and how? Explain the process. What does it not succeed in modifying, and why? Explain the process. From all this, does it appear that Java handles objects and primitive variables differently, from an argument passing point of view?

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

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)

ii)

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”. 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" ); } }

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.

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

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?

Related Documents

Core Java
May 2020 24
Core Java
October 2019 43
Core Java
November 2019 30