num1=num2=num3;
A.
public class Test { public static void main(String[] args) { int num1=10; int num2=20; int num3=30; num1=num2=num3; System.out.println(num1); System.out.println(num2); System.out.println(num3); } }
Q. Here is a code. 'num2+=10' is not working in this code. Fix it to increase the value of 'num2.'
public class Test { public static void main(String[] args) { int num1=0, num2=0; boolean result; result = (num1+=10)<0 && (num2+=10)>0; System.out.println("result="+result); System.out.println("num1="+num1+", num2="+num2); result = (num1+=10) > 0 || (num2+=10) >0; System.out.println("Result = " + result); System.out.println("num1 = " + num1 + ", num2 = " + num2); } }
A.
public class Test { public static void main(String[] args) { int num1=0, num2=0; boolean result; num1+=10; num2+=10; result = (num1<0) && (num2>0); System.out.println("result = " + result); System.out.println("num1 = " + num1 + ", num2 = " + num2); num1+=10; num2+=10; result = (num1>0) || (num2>0); System.out.println("result = " + result); System.out.println("num1 = " + num1 + ", num2 = " + num2); } }
Q. Write a program to calculate: {(25*5)+(36-4)-72}/5
A.
public class Test { public static void main(String[] args) { int ans = ((25*5)+(3604)-72)/5; System.out.println(ans); } }
Q. Write a program for calculating 3+6, 3+6+9, 3+6+9+14 with minimum times of adding.
A.
public class Test { public static void main(String[] args) { int result=3+6; System.out.println(result); result +=9; System.out.println(result); result +=12; System.out.println(result); } }
Q. A={(25+5)+(36/4)-72}*5, B={(25*5)+(36-4)+71}/4, C=(128/4)*2. Write a program when A>B>C, shows true. In other cases, it shows false.
A.
public class Test { public static void main(String[] args) { int A = ((25+5)+(36/4)-72)*5; int B = ((25*5)+(36-4)+71)/4; int C = (128/4)*2; boolean isTrue; isTrue = (A>B) && (B>C); System.out.println(isTrue); } }
Q. Write a program to show 2's complement of the digit 7.
A.
public class Test { public static void main(String[] args) { int x=7; x = ~x; x = x+1; System.out.println(x); } }
Q. Write a program to check a third bit digit and fifth from left of an integer 15678.
A.
public class Test { public static void main(String[] args) { int n=15678; System.out.println( (n>>2)&1 ); System.out.println( (n>>4)&1 ); } }
Q. The result of perator '<<' multiplies the operand by 2 times.
However, sometimes it changes MSB and shows wrong answers.
write an example using positive and negative integers to show the errors.
A.
public class Test { public static void main(String[] args) { int num=7; int bitset = 1<<30; num |= bitset; System.out.println(num); System.out.println(num <<=1); num=-12; bitset = ~0; bitset ^= (1<<30); num &= bitset; System.out.println(num); System.out.println(num <<= 1); } }
No comments:
Post a Comment