Primitive Assignments,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
Primitive Assignments

Primitive assignment is basically use for assigning the value like equal(=) sign is used for the assigning a value to a variable and it\'s cleverly named the assignment operator. These compound assignment operators have a couple of special properties we\'ll look at in this section.

Compound Assignment Operators :  
 
=      |    *=     |     /=      |    %=    |
----------------------------------------------
+=     |    -=     |     <<=     |    >>=   |
----------------------------------------------
>>>=   |     &=    |      ^=     |    |=    |
----------------------------------------------


We can assign a primitive variable using a literal or the result of an expression.

int x = 7; // literal assignment
int y = x + 2; // assignment with an expression (including a literal)
int z = x * y; // assignment with an expression

The compiler automatically narrows the literal value to a byte. In other words, the compiler puts in the cast. The preceding code is identical to the
following:

byte b = (byte) 27; // Explicitly cast the int literal to a byte
           Divide a short by a byte and you\'ll
get an int. OK, now we\'re at the weird part.

byte b = 3; // No problem, 3 fits in a byte
byte c = 8; // No problem, 8 fits in a byte
byte d = b + c; // Should be no problem, sum of the two bytes
// fits in a byte
The last line won\'t compile! You\'ll get the following error:
TestBytes.java:5: possible loss of precision
found : int
required: byte
byte c = a + b;
^

More interview questions and answers

Which word is reserved word in the Java programming language in the following option?

Which one of these lists contains only Java programming language keywords?

Which two are keywords?

A top level class without any modifier is accessible by....... (Select most appropriate answer)

Which three are valid declarations of a char?

Which two are valid declarations of a String?

Which one is a valid declaration of a boolean?

What is the numerical range of a char?

Which three are valid declarations of a float?

From below select three correct statement in Java.

Which three are legal array declarations?

public class myprog { public static void main(String [] args) { int [] [] [] a = new int [3] [] []; int x,y; a[0] = new int[3][]; a[1] = new int[3][]; a[2] = new int[4][]; for (x=0; xGiven the following, public class myprog { public static void main(String [] args) { byte [][] big = new byte [8][8]; byte [][] x = new byte [3][2]; byte x3 = 6; byte x2 [][][][] = new byte [3][4][2][3]; .................................. } } which of the following lines of code could be inserted at dotted line , and still allow the code to compile?

Which two will declare an array and initialize it with Ten numbers?

Which will legally declare, construct, and initialize an array?

Which four describe the correct default values for array elements of the types indicated?

Given the following, public class myhorse { public static void main(String [] args) { Horse [][] theHorse = new Horse[3][]; System.out.println(theHorse[2][0].toString()); } } class Horse {} what is the result?

Given the following, public class myprog { public static void main(String [] args) { String names [] = new String[10]; for (int i=0; i < args.length; i++) names[i] = args[i]; System.out.println(names[2]); } } and the command line invocation is java i a b what is the result?

Given the following, public class CommandArgs { public static void main(String [] args) { String s1 = args[1]; String s2 = args[2]; String s3 = args[3]; String s4 = args[4]; System.out.print(" args[2] = " + s2); } } and the command-line invocation, java CommandArgs 1 2 3 4 , what is the result?

Given the following, public class CommandArgsTwo { public static void main(String [] argh) { String [] args; int x; x = argh.length; for (int y = 1; y <= x; y++) { System.out.print(" " + argh[y]); } } } and the command-line invocation, java CommandArgsTwo 1 2 3

Given the following, public class CommandArgsThree { public static void main(String [] args) { String [][] argCopy = new String[2][2]; int x; argCopy[0] = args; x = argCopy[0].length; for (int y = 0; y < x; y++) { System.out.print(" " + argCopy[0][y]); } } } and the command-line invocation, java CommandArgsThree 1 2 3 what is the result?

What result comes when we compile and run following code: class R4RSCJPTest { public static void main(String args[]) { StringBuffer sb1 = new StringBuffer("Vivek"); StringBuffer sb2 = new StringBuffer("Vivek"); String s1 = "Vivek"; String s2 = "Vivek"; System.out.print(s1==s2); System.out.print(" "+(sb1==sb2)); System.out.print(" "+(s1.equals(s2))); System.out.print(" "+(sb1.equals(sb2))); System.out.print(" "+(s1.equals(sb2))); System.out.print(" "+(sb1.equals(s2))); } }

What result comes when we compile and run following code: class R4RSCJPTest { public static void main(String args[]) { String s1 = "Vivek"; String s2 = "Vivek"; System.out.print(" "+s1==s2); System.out.print(" "+(s1==s2)); } }

What result comes when we compare and run followig code: class R4RSCJPTest { public static void main(String args[]) { System.out.println("R4R Welcomes You!".substring(3)); } }

What is the most restrictive access modifier that will allow members of one class to have access to members of another class in the same package?

Given a method in a protected class, what access modifier do we use to restrict access to that method to only the other members of the same class?

What result come when we compile and run following code: class R4RSCJPTest { public static void main(String arg[]) { StringBuffer sb[]={"R","V"}; System.out.println(sb[0]+" , "+sb[1]); } }

Given the following, abstract class myprog { abstract short p1() ; short m2() { return (short) 786; } } abstract class myprog1 extends myprog { // missing code ? short p1() { return (short) 78; } } which three of the following statements are true?

Which two of the following are legal declarations for nonnested classes and interfaces?

How many of the following are legal method declarations? A> protected abstract void m1(); B> static final void m1(){} C> transient private native void m1() {} D> synchronized public final void m1() {} E> private native void m1(); F> static final synchronized protected void m1() {}

Given the following, package testpkg.p1; public class ParentUtil { public int x = 420; protected int doStuff() { return x; } } package testpkg.p2; import testpkg.p1.ParentUtil; public class ChildUtil extends ParentUtil { public static void main(String [] args) { new ChildUtil().callStuff(); } void callStuff() { System.out.print("this " + this.doStuff() ); ParentUtil p = new ParentUtil(); System.out.print(" parent " + p.doStuff() ); } } which statement is true?

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?

R4R provides free online tutolrails ,examples for Freshers and experts .We cover core java, tutorials, spring, Hibernate, python, struts, struts 2,c language, spring boot,PHP,MYSQL,C#,ASP.NET,WPF,WCF,Photoshop , JSP, programming, example, Interview questions and answer