Java Programming - Operators and Assignments
Why should I learn to solve Java Programming questions and answers section on "Operators and Assignments"?
Learn and practise solving Java Programming questions and answers section on "Operators and Assignments" to enhance your skills so that you can clear interviews, competitive examinations, and various entrance tests (CAT, GATE, GRE, MAT, bank exams, railway exams, etc.) with full confidence.
Where can I get the Java Programming questions and answers section on "Operators and Assignments"?
IndiaBIX provides you with numerous Java Programming questions and answers based on "Operators and Assignments" along with fully solved examples and detailed explanations that will be easy to understand.
Where can I get the Java Programming section on "Operators and Assignments" MCQ-type interview questions and answers (objective type, multiple choice)?
Here you can find multiple-choice Java Programming questions and answers based on "Operators and Assignments" for your placement interviews and competitive exams. Objective-type and true-or-false-type questions are given too.
How do I download the Java Programming questions and answers section on "Operators and Assignments" in PDF format?
You can download the Java Programming quiz questions and answers section on "Operators and Assignments" as PDF files or eBooks.
How do I solve Java Programming quiz problems based on "Operators and Assignments"?
You can easily solve Java Programming quiz problems based on "Operators and Assignments" by practising the given exercises, including shortcuts and tricks.
- Operators and Assignments - Finding the output
- Operators and Assignments - Pointing out the correct statements
class PassA
{
public static void main(String [] args)
{
PassA p = new PassA();
p.start();
}
void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}
long [] fix(long [] a3)
{
a3[1] = 7;
return a3;
}
}
Output: 15 15
The reference variables a1 and a3 refer to the same long array object. When the [1] element is updated in the fix() method, it is updating the array referred to by a1. The reference variable a2 refers to the same array object.
So Output: 3+7+5+" "3+7+5
Output: 15 15 Because Numeric values will be added
class Test
{
public static void main(String [] args)
{
Test p = new Test();
p.start();
}
void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}
boolean fix(boolean b1)
{
b1 = true;
return b1;
}
}
The boolean b1 in the fix() method is a different boolean than the b1 in the start() method. The b1 in the start() method is not updated by the fix() method.
class PassS
{
public static void main(String [] args)
{
PassS p = new PassS();
p.start();
}
void start()
{
String s1 = "slip";
String s2 = fix(s1);
System.out.println(s1 + " " + s2);
}
String fix(String s1)
{
s1 = s1 + "stream";
System.out.print(s1 + " ");
return "stream";
}
}
When the fix() method is first entered, start()'s s1 and fix()'s s1 reference variables both refer to the same String object (with a value of "slip"). Fix()'s s1 is reassigned to a new object that is created when the concatenation occurs (this second String object has a value of "slipstream"). When the program returns to start(), another String object is created, referred to by s2 and with a value of "stream".
class BitShift
{
public static void main(String [] args)
{
int x = 0x80000000;
System.out.print(x + " and ");
x = x >>> 31;
System.out.println(x);
}
}
Option A is correct. The >>> operator moves all bits to the right, zero filling the left bits. The bit transformation looks like this:
Before: 1000 0000 0000 0000 0000 0000 0000 0000
After: 0000 0000 0000 0000 0000 0000 0000 0001
Option C is incorrect because the >>> operator zero fills the left bits, which in this case changes the sign of x, as shown.
Option B is incorrect because the output method print() always displays integers in base 10.
Option D is incorrect because this is the reverse order of the two output numbers.
class Equals
{
public static void main(String [] args)
{
int x = 100;
double y = 100.1;
boolean b = (x = y); /* Line 7 */
System.out.println(b);
}
}
The code will not compile because in line 7, the line will work only if we use (x==y) in the line. The == operator compares values to produce a boolean, whereas the = operator assigns a value to variables.
Option A, B, and D are incorrect because the code does not get as far as compiling. If we corrected this code, the output would be false.