Conditional Operator,Java, C ,C++, ASP, ASP.net C# ,Struts ,Questions & Answer, Struts2, Ajax, Hibernate, Swing ,JSP , Servlet, J2EE ,Core Java ,Stping, VC++, HTML, DHTML, JAVASCRIPT, VB ,CSS, interview ,questions, and answers, for,experienced, and fresher
Conditional Operator

The condition operator is is a basically a tenantry operator its mainly used for the boolean expressions, its similar as a if statement but some difference  its not executing a block of code if the test is true, a conditional operator will assign a value to a variable, we can also say the goal of the conditional operator is to decide which of two values to assign to a variable.


someVariable = (boolean expression) ? value to assign if true : value to assign if false

We have a some code for the conditional operator :

class Salary {
public static void main(String [] args) {
int numOfPets = 3;
String status = (numOfPets<4)?\"Pet limit not exceeded\":\"too many pets\";
System.out.println(\"This pet status is \" + status);
}
}

A conditional operator starts with the boolean operator followed by two possible values for the variable to the left of the conditional operator.The first value (the one to the left of the colon) is assigned if the conditional (boolean) test is true, and the second value is assigned if the conditional test is false.

You can even nest conditional operators into one statement.

class AssignmentOps {
public static void main(String [] args) {
int sizeOfYard = 10;
int numOfPets = 3;
String status = (numOfPets<4)?\"Pet count OK\"
:(sizeOfYard > 8)? \"Pet limit on the edge\"
:\"too many pets\";
System.out.println(\"Pet status is \" + status);
}
}

More interview questions and answers
Given the following, interface Count { short counter = 0; void countUp(); } public class TestCount implements Count { public static void main(String [] args) { TestCount t = new TestCount(); t.countUp(); } public void countUp() { for (int x = 6; x>counter; x--, ++counter) { System.out.print(" " + counter); } } } what is the result?
Given the following, import java.util.*; public class NewTreeSet2 extends NewTreeSet { public static void main(String [] args) { NewTreeSet2 t = new NewTreeSet2(); t.count(); } } protected class NewTreeSet { void count() { for (int x = 0; x < 7; x++,x++ ) { System.out.print(" " + x); } } } what is the result?
Given the following, .................................. public class NewTreeSet extends java.util.TreeSet{ public static void main(String [] args) { java.util.TreeSet t = new java.util.TreeSet(); t.clear(); } public void clear() { TreeMap m = new TreeMap(); m.clear(); } } which two statements, added independently at Dotted line , allow the code to compile?
Which two are valid declarations within an interface?
Which of the following class level (nonlocal) variable declarations will not compile?
Given the following, 1. interface DoMath { 2. double getArea(int rad); } 3. 4. interface MathPlus { double getVol(int b, int h); } ---------------------------------- .................................. which two code fragments inserted at Dash and Dotted lines will compile?
Which three are valid method signatures in an interface?
Which two statements are true for any concrete class implementing the java.lang.Runnable interface?
What result come when we compile and run following code: class R4RSCJPTest { public static void main(String ds[]) { String str="Vivek"; str.concat("Kumar"); str+=" Agarwal"; System.out.println( str + " Welcomes You! At R4R."); } }
Given the following, interface Base { boolean m1 (); byte m2(short s); } which two code fragments will compile?
Properties of main()
java.lang.Runnable
Java Operators
Assignment Operators
Primitive Assignments
Assigning Floating-Point Numbers
Assigning a Literal That Is Too Large for the Variable
Assigning One Primitive Variable to Another Primitive Variable
Reference Variable Assignments
Assigning One Reference Variable to Another
Comparison Operators
instanceof Comparison
Equality Operators
Equality for Primitives
Equality for Reference Variables
What result comes when we compile and run following code: class R4RSCJPTest { public static void main(String arg[]) { if("welcome".indexOf("w",0) ==0) { System.out.println("Equal"); } else { System.out.println("Not Equal"); } } }
What result comes when we compile and run following code: class R4RSCJPTest { public static void main(String arg[]) { if("welcome".indexOf("w",-3) ==0) { System.out.println("Equal"); } else { System.out.println("Not Equal"); } } }
What result comes when we compile and run following code: class R4RSCJPTest { public static void main(String arg[]) { if("welcome".indexOf("a",1) ==-1) { System.out.println("Equal"); } else { System.out.println("Not Equal"); } } }
What result comes when we compile and run following code: class R4RSCJPTest { public static void main(String arg[]) { if("welcome".lastIndexOf("e",9) ==6) { System.out.println("Equal"); } else { System.out.println("Not Equal"); } } }
What result come when we compile and run following code: class R4RSCJPTest { public static void main(String arg[]) { if("welcome".lastIndexOf("e",-3) ==1) { System.out.println("Equal"); } else { System.out.println("Not Equal"); } } }
Select correct statements from followings:
What result come when we compile and run following code: class R4RSCJPTest { public static void main(String [] args) { int i = 3; double j = 3.2; boolean boo = (i = j); System.out.println("Value of boo is " + boo); } }
What result come when we compile and run following code: class R4RSCJPTest { public static void main(String [] args) { int i = 3; double j = 3.2; boolean boo = (i == j); System.out.println("Value of boo is " + boo); } }
What result come when we compile and run following code: class R4RSCJPTest { public static void main(String [] args) { int i= 1; int j= 5; for (int k = 1; k <= 4; k++) { if (( ++i > 3 ) || (--j > 3)) { i++; } } System.out.println("Value of i is " +i + ", Value of j is " +j); } }
What result come when we compile and run following code: class R4RSCJPTest { public static void main(String [] args) { boolean b1 = true; boolean b2 = false; boolean b3 = true; if ( b1 & b2 | b2 & b3 | b2 ) System.out.print("First if statement will executed."); if ( b1 & b2 | b2 & b3 | b2 | b1 ) System.out.println("Second if statement will executed."); } }
What result come when we compile and run following code: class R4RSCJPTest { public static void main(String [] args) { int age=20; String str = (age<18)?"Teenager":(age<25)?"Young":"Adult"; System.out.println(str); } }
What result come when we compile and run following code: class R4RSCJPTest { public static void main(String [] args) { R4RSCJPTest test = new R4RSCJPTTest(); test.start(); } void start() { boolean boo1 = true; boolean boo2 = fix(boo1); System.out.println(boo1 + " " + boo2); } boolean fix(boolean boo1) { boo1 = false; return boo1; } }
What result come when we compile and run following code: class R4RSCJPTest { public static void main(String [] args) { R4RSCJPTest test = new R4RSCJPTest(); test.start(); } void start() { int i = 2; int j = 4; System.out.print(" " + 1 + 3 + " "); System.out.print(i + 4 + j -1); System.out.print(vivek() + i + j + " "); System.out.print(" " + i + j + " "); System.out.println(i + j + vivek()); } String vivek() { return "vivek"; } }
What result come when we compile and run following code: lass R4RSCJPTest { public static void main(String [] args) { R4RSCJPTest test = new R4RSCJPTest(); test.start(); } void start() { long [] arr1 = {2,4,6}; long [] arr2 = foo(arr1); System.out.print(arr1[0] + arr1[1] + arr1[2] + " "); System.out.println(arr2[0] + arr2[1] + arr2[2]); } long [] foo(long [] arr3) { arr3[1] = 8; return arr3; } }
Arithmetic Operators
String concentration Operator
Increment and Decrement
Shift Operators
Bitwise Operators
Bitwise Complement Operator
Conditional Operator
Primitive Casting
Casting Primitives
Logical Operators
Short-Circuit Logical Operators
Logical Operators (not Short-Circuit)
Passing Variables into Methods
Passing Object Reference Variables
Does Java Use Pass-By-Value Semantics?
What result come when we compile and run following code: class Test1 { byte b; } class R4RSCJPTest { public static void main(String [] args) { R4RSCJPTest p = new R4RSCJPTest(); p.start(); } void start() { Test1 t1 = new Test1(); System.out.print(t1.b + " "); Test1 t2 = foo(t1); System.out.println(t1.b + " " + t2.b); } Test1 foo(Test1 tt1) { tt1.b = 21; return tt1; }
Passing Primitive Variables
What result come when we compile and run following code: class R4RSCJPTest { public static void main(String args[]) { R4RSCJPTest t = new R4RSCJPTest(); t.start(); } void start() { String str1 = "welcome"; String str2 = foo(str1); System.out.println(str1 + " " + str2); } String foo(String str1) { str1 = str1 + "user"; System.out.print(str1 + " "); return "user"; } }
What is the result of (1 & 3)?
What is the result of (1 | 3)?
What is the result of (1 << 2)?
What is the resulting value of (new String(“fred”) instanceof Object)?
Which two are equal?
Given the following, import java.awt.*; class Ticker extends Component { public static void main (String [] args) { Ticker t = new Ticker(); ........................... } } which two of the following statements, inserted independently, could legally be inserted into dotted line of this code?
Given the following, class Equals { public static void main(String [] args) { int a = 100; double b = 100.1; boolean x = (a = b); System.out.println(x); } } what is the result?
Given the following, import java.awt.Button; class CompareReference { public static void main(String [] args) { float f = 42.0f; float [] f1 = new float[2]; float [] f2 = new float[2]; float [] f3 = f1; longx = 42; f1[0] = 42.0f; } } which three statements are true?
Given the following, class BitShift { public static void main(String [] args) { int x = 0x80000000; System.out.print(x + " and "); x = x >>> 31; System.out.println(x); } } what is the output from this program?
Given the following, class Bitwise { public static void main(String [] args) { int x = 11 & 9; int y = x ^ 3; System.out.println( y | 12 ); } } what is the result?
Which of the following are legal lines of code?
Given the following, class Test { public static void main(String [] args) { int a= 0; int b= 0; for (int c = 0; c < 5; c++) { if (( ++a > 2 ) || (++b > 2)) { b++; } } . System.out.println(a + " " + b); } } what is the result?
Given the following, class Test { public static void main(String [] args) { int b= 0; int b= 0; for (int c = 0; c < 5; c++) { if (( ++a > 2 ) && (++b > 2)) { a++; } } System.out.println(a + " " + b); } } What is the result?
Given the following, class SSBool { public static void main(String [] args) { boolean p1 = true; boolean p2 = false; boolean p3 = true; if ( p1 & p2 | p2 & p3 | p2 ) System.out.print("ok "); if ( p1 & p2 | p2 & p3 | p2 | p1 ) System.out.println("dokey"); } } what is the result?
Given the following, 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(); } } 16. 17. void test() { 18. if ( b[0] && b[1] | b[2] ) 19. count++; 20. if ( b[1] && b[(++count - 2)] ) count += 7; 2System.out.println("count = " + count); } } what is the result?
Given the following, class Test { static int s; public static void main(String [] args) { Test p = new Test(); p.start(); System.out.println(s); } void start() { int x = 7; twice(x); System.out.print(x + " "); } void twice(int x) { = x*2; s = x; } } what is the result
What result come when we compile and run following code: public class R4RSCJPTest { final static short i = 4; public static int j = 0; public static void main(String [] args) { for (int k=0; k <= 4; k++) { switch (k) { case j: System.out.print(" 0 "); case i-3: System.out.print(" 1 "); case i-2: System.out.print(" 2 "); case i-1: System.out.print(" 3 "); case i: System.out.print(" 4 "); } } } }
What result come when we compile and run following code: public class R4RSCJPTest { final static short i = 3; public static int j = 1; public static void main(String [] args) { for (int k=1; k <= 4; k++) { switch (k) { case i: System.out.print(" 0 "); case i-1: System.out.print(" 1 "); case i-2: System.out.print(" 2 "); case i-3: System.out.print(" 3 "); } } } }
What result come when we compile and following code: public class R4RSCJPTest { static boolean boo; public static void main(String [] args) { short st = 24; if ( st < 30 & !boo ) st++; if ( st > 30 ) ; else if ( st > 38 ) { st -= 6; st++; } else ++st; System.out.println(st); } }
Given the following, class Test { public static void main(String [] args) { Test p = new Test(); p.start(); } void start() { boolean p1 = false; boolean p2 = fix(p1); System.out.println(p1 + " " + p2); } boolean fix(boolean b1) { b1 = true; return b1; } } what is the result?
Given the following, 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"; } } what is the result
Given the following, 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 + " "); 14. System.out.println(a + b + foo()); } String foo() { return "foo"; } } what is the result?
Given the following, 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; } } what is the result?
Given the following, class Two { byte x; } class PassO { public static void main(String [] args) { PassO p = new PassO(); p.start(); } void start() { Two t = new Two(); System.out.print(t.x + " "); Two t2 = fix(t); System.out.println(t.x + " " t2.x); } Two fix(Two tt) { tt.x = 42; return tt; } } what is the result?
What result come when we compile and run following code: public class R4RSCJPTest { final static short i = 3; public static int j = 1; public static void main(String [] args) { for (int j=1; j <= 4; j++) { switch (j) { case i: System.out.print(" 0 "); case i-1: System.out.print(" 1 "); default: System.out.print("def "); case i-2: System.out.print("2 ");break; case i-3: System.out.print("3 "); } } } }
What result come when we compile and run following code: public class R4RSCJPTest { public void loop() { int i= 3; while ( 1 ) { System.out.print(+ (i + 1)); } } }
What result come when we compile and run following code: public class R4RSCJPTest { public static void main(String args[]) { int i = 3; do while ( i < 3 ) System.out.print("Value of i is " + i); while ( i > 3 ) ; } }
What result come when we compile and run following code: class R4RSCJPTest { static int i; public static void main(String [] args) { R4RSCJPTest test = new R4RSCJPTest(); test.start(); System.out.println(i); } void start() { int j = 3; foo(j); j = j^2; System.out.print(j + " "); } void foo(int j) { j = j*2; i = j; } }
What result come when we compile and run following code: public class R4RSCJPTest { static boolean boo1, boo2; public static void main(String [] args) { int i = 0; if ( !boo1 ) { if ( !boo2 ) { boo1 = false; i++; if ( 0 < i ) { i++; } boo2 = !boo2; if ( boo1 ) i = i + 10; else if ( boo2 == false ) i = i + 100; else if ( boo1 | boo2 ) i = i + 1000; } } System.out.println(i); } }
Writing Code Using if statement
legal arguments for if Statements
Writing Code Using Switch Statement
Legal Arguments to switch and case
default, break, and fall-through in switch blocks
The Default Case
creating a switch-case statement.
Select correct data types that we used in switch statements.
Select two correct for loops when we insert those in following code output will be like that: i = 0 i = 1 class R4RSCJPTest { public static void main(String args[]) { //insert both for loop here { System.out.println("i = "+ i); } } }
What result come when we compile and run following code: class R4RSCJPTest { public static void main(String args[]) { int i = 2; boolean boo = false; outer: while (!boo) { i++; inner: for (int j = 1; j < 7; j++) { i += j; if (j == 4) continue inner; break outer; } continue outer; } System.out.println("Value of i is " +i); } }
What result come when we compile and run following code: class R4RSCJPTest { public static void main(String args[]) { int i = 9; label: if (i > 3) { i--; continue label; } System.out.println("i is " + i); } }
What result come when we compile and run following code: class R4RSCJPTest { public static void main(String args[]) { int i = 9; label: while (i > 3) { i--; continue label; } System.out.print("i is " + i); } }
Writing code using loops.
Using while loops