Some practice questions related to Core Java with answers .
Core Java Important Notes for interview preparationFull description
Full description
Full description
Full description
Hand Written Note by Natraj Sir
Description complète
grafos y sus aplicaciones.Descripción completa
javaDescription complète
javaDescripción completa
Full description
java
CORE JAVA \u2013 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) { \u2026\u2026\u2026 } protected int getEntityId() { \u2026\u2026\u2026 } } class EntityTest { public static void main(String[] args) { \u2026\u2026\u2026 Entity mWorkEntity = null; mWorkEntity.setEntityId(10); System.out.println(mWorkEntity.getEntityId());
}
3.
}
return;
\u2026\u2026\u2026\u2026
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( ); //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 \u201cmWorkEntit of the \u201cmWorkEntityCount\u201d 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 ) { \u2026\u2026\u2026\u2026 // method body \u2026\u2026\u2026\u2026 }
}
// other methods \u2026\u2026\u2026\u2026 \u2026\u2026\u2026\u2026
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 prog 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 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 displayMessa 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?