Tuesday, November 17, 2015
Saturday, October 31, 2015
Strings in java are called by value not reference
Why?
Because String is immutable.
Because String is immutable.
Assert in java
which line is an example of an inappropriate use of assertions? | |||||||||||||||||
Answer: Option D
Explanation:
Assert statements should not cause side effects. Line 22 changes the value of z if the assert statement is false.
Option A is fine; a second expression in an assert statement is not required.
Option B is fine because it is perfectly acceptable to call a method with the second expression of an assert statement.
Option C is fine because it is proper to call an assert statement conditionally.
|
Output of program
| |||||||||||||||||
Answer: Option D
Explanation:
The foo() method returns void. It is a perfectly acceptable method, but because it returns void it cannot be used in an assert statement, so line 18 will not compile.
Learn more problems on : Assertions
Discuss about this problem : Discuss in Forum
http://www.indiabix.com/online-test/java-programming-test/61 |
Friday, October 30, 2015
Thread And Runnable both have run method which one will be called?
| |||||||||||||||||
Answer: Option A
Explanation:
If a Runnable object is passed to the Thread constructor, then the run method of theThread class will invoke the run method of the Runnable object.
In this case, however, the run method in the Thread class is overridden by the run method in MyThread class. Therefore the run() method in MyRunnable is never invoked.
Both times, the run() method in MyThread is invoked instead.
Reference: http://www.indiabix.com/online-test/java-programming-test/61
|
IllegalThreadStateException
| |||||||||||||||||
Answer: Option B
Explanation:
When the start() method is attempted a second time on a single Thread object, the method will throw an IllegalThreadStateException (you will not need to know this exception name for the exam). Even if the thread has finished running, it is still illegal to call start() again.
|
short circuit operator in java
class BoolArray
{
boolean [] b = new boolean[3];
int count = 0;
void set(boolean [] x, int i)
{
x[i] = true;
++count;
}
public static void main(String [] args)
{
BoolArray ba = new BoolArray();
ba.set(ba.b, 0);
ba.set(ba.b, 2);
ba.test();
}
void test()
{
if ( b[0] && b[1] | b[2] )
count++;
if ( b[1] && b[(++count - 2)] )
count += 7;
System.out.println("count = " + count);
}
}
Result count = 3
Why?
The reference variables b and x both refer to the same boolean array. count is incremented for each call to the set() method, and once again when the first if test istrue. Because of the && short circuit operator, count is not incremented during the second if test.
Output of program
class SC2
{
public static void main(String [] args)
{
SC2 s = new SC2();
s.start();
}
void start()
{
int a = 3;
int b = 4;
System.out.print(" " + 7 + 2 + " ");
System.out.print(a + b);
System.out.print(" " + a + b + " ");
System.out.print(foo() + a + b + " ");
System.out.println(a + b + foo());
}
String foo()
{
return "foo";
}
}
Result
72 7 34 foo34 7foo
Description:
Because all of these expressions use the + operator, there is no precedence to worry about and all of the expressions will be evaluated from left to right. If either operand being evaluated is a String, the + operator will concatenate the two operands; if both operands are numeric, the + operator will add the two operands.
Subscribe to:
Comments (Atom)
