# Query

Tayal Rohit
Greenhorn
Posts: 12
Can any one answer a question i found in a test , but not sure of answer.the last questions*(no -6) and please reply if u r agree with the answers i gave.

1.What is the output of the following class? Briefly explain your answer.

public class Bar {
private static String a;
private static void foo(String a){
a = "second";
}
public static void main(String[] argv){
a = "first";
System.out.println(a);
foo(a);
System.out.println(a);
}
}

First
Second

In method main � first out put statement will print the value of a
And after that foo() get calles that changed the value of a = �second�.

2.Override java.lang.Object.equals in the following class, so that equality is based on values rather than references.

public class Simple {
private int digits;
private String text;

public Simple(int i, String s) {
digits = i;
text = s;
}
}

public boolean equals(Object a)
{
boolean res = true;
try
{
Simple aCast = (Simple)a;

If(this.digits!=aCast.digits)
{
res= false;
}
If(!this.test.equals(aCast.a))
{
res= false;
}
}catch(ClasCastException cce)
{
res = false;
}
return false;

}
}

public class Test4 {

static void doHelloGoodbye(Base b){
b.greeting();
b.farewell();
}

public static void main(String args[]){
Sub s = new Sub();
doHelloGoodbye(s);
}
}

class Base{
static void greeting(){
System.out.println("Hello");
}
void farewell(){
System.out.println("Goodbye");
}
}

class Sub extends Base{
static void greeting(){
System.out.println("Hi");
}
void farewell(){
System.out.println("See ya!");
}
}

Hello
See ya

This is because object is of Sub type even when it is casted as Base type and sub class has overrideden the methods.
But will cause a warning while compilation as static method is being called as non static element.

4.What distinguishes RuntimeException from other subclasses of Exception?

Runtime exceptions:
�A method signature does not need to declare runtime exceptions
�A caller to a method that throws a runtime exception is not forced to catch the runtime exception
�Runtime exceptions extend from RuntimeException or Error
Other sub class of Exceptions:
�A method must declare each checked exception it throws
�A caller to a method that throws a checked exception must either catch the exception or throw the exception itself
�Checked exceptions extend from Exception
And we should always catch a checked exception once you reach a point where our code can make a meaningful attempt at recovery. However, it is best not to catch runtime exceptions. Instead, we should allow runtime exceptions to bubble up to where we can see them.

5.Simplify the following method

public static int jabberwock(boolean frumious, boolean manxome,
boolean uffish){
if (!frumious){
if (manxome) {
if (uffish){
return JUBJUB_BIRD;
}
else if (!manxome) {
return BANDERSNATCH;
}
else{
return JUBJUB_BIRD;
}
}
else {
return JUBJUB_BIRD;
}
}
else if (!uffish){
if (!manxome){
return BANDERSNATCH;
}
}
else if(!manxome){
return JUBJUB_BIRD;
}
if (!(uffish || !manxome)) {
return BANDERSNATCH;
}
return JUBJUB_BIRD;
}

public static int jabberwock(boolean frumious, boolean manxome,
boolean uffish){
if (!frumious){
if (manxome) {
if (uffish){
return JUBJUB_BIRD;
}

else{
return JUBJUB_BIRD;
}
}
else {
return JUBJUB_BIRD;
}
}
else if (!uffish){
if (!manxome){
return BANDERSNATCH;
}
}
else if(!manxome){
return JUBJUB_BIRD;
}

return JUBJUB_BIRD;
}

6.The following code is a complete application, which accesses a **shared** SQL database. This code seemed ok in test but causes some unrelated updates of salary to be occasionally lost in production. Why could this happen? What options are there to resolve this problem? Hint: Focus on coreLogic.

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Operation {
private Connection c;

private void coreLogic(long empId) throws SQLException {
BigDecimal salary = selectSalary(empId);
salary = indexSalary(salary);
}

public void execute(Connection aConn, long empId)
throws SQLException {
c = aConn;
c.setAutoCommit(false);
try {
coreLogic(empId);
c.commit();
} catch (SQLException e) {
c.rollback();
throw e;
}
}

private BigDecimal selectSalary(long empId) throws SQLException {
PreparedStatement ps = c.prepareStatement(
"SELECT salary FROM emp WHERE emp_id = ?");
ResultSet rs = null;
try {
ps.setLong(1, empId);
rs = ps.executeQuery();
rs.next();
return rs.getBigDecimal("salary");
} finally {
if (rs != null) {
rs.close();
}
ps.close();
}
}

private BigDecimal indexSalary(BigDecimal salary) {
return salary.multiply(new BigDecimal("1.1"));
}

private void updateSalary(long empId, BigDecimal salary)
throws SQLException {
PreparedStatement ps = c.prepareStatement(
"UPDATE emp SET salary = ? WHERE emp_id = ?");
try {
ps.setBigDecimal(1, salary);
ps.setLong(2, empId);
ps.executeUpdate();
} finally {
ps.close();
}
}

public static void main(String args[]) throws Exception {
Class.forName(args[0]);
Connection c = DriverManager.getConnection(args[1], args[2], args[3]);

Operation op = new Operation();
op.execute(c, Long.parseLong(args[4]));
}
}

Sample DDL is for emp is:

CREATE TABLE emp ( emp_id NUMERIC PRIMARY KEY,
salary NUMERIC NOT NULL);

Mark Herschberg
Sheriff
Posts: 6037
I'm going to move this to Java in General Intermediate (as it doesn't obviously fit into any other category).

--Mark