Question

In: Computer Science

Find the output for the following statements. System.out.println(3%2+1+(3/2)); char ch = 69; System.out.println(ch); int x =...

Find the output for the following statements.

  • System.out.println(3%2+1+(3/2));
  • char ch = 69;
    System.out.println(ch);
  • int x = 30;

int y = 4;

double z = x/y;

System.out.println(z);

Solutions

Expert Solution

Please up vote ,comment if any query . Thanks for Question .

Note : check attached image for output .

Answer :

       1. System.out.println(3%2+1+(3/2)); //output will 3 how ?

        how ? : -

          first it will solve bracket so 3/2 = 1 both operand integer so 3/2 will be 1 now equation is

         System.out.println(3%2+1+1);

         in Java operator precedence + come after % in precedence

           System.out.println(1+1+1); //3%2=1

            System.out.println(3); //output

         

        2. char ch = 69; //69 ascii value of 'E'
             System.out.println(ch); //it will print E on console

         3. int x = 30;
            int y = 4;
            double z = x/y; z=30/4

       by mathematic the result should be 7.5 but output will be 7.

       why both operand x and y are integer type so result will also integer type . to get double

        we should type cast the result z= (double)x/y = 7.5
        System.out.println(z); //it will print 7 without type casting

Output :

Please up vote ,comment if any query .


Related Solutions

int a = 3; int b = -2; if((a>0)&&(b>0)){ if (a>b) { System.out.println("A"); } else {...
int a = 3; int b = -2; if((a>0)&&(b>0)){ if (a>b) { System.out.println("A"); } else { System.out.println("B"); } } else if ((b<0)||(a<0)) { System.out.println("C"); } else { System.out.println("D"); }
public static int punishOrMercy(char direction, int reward) Output: int which will be the value of zero...
public static int punishOrMercy(char direction, int reward) Output: int which will be the value of zero or the initial reward. Functionality: After the result of the reward function is stored, this function should be called. The goal of this function is to help the robot in case it faced a lot of damage in the current step. However, this function will help the robot only if the robot’s reward was negative and the direction that the user inputted was up....
what is the output? int main ( ) { int a = 3, b= 2, c=...
what is the output? int main ( ) { int a = 3, b= 2, c= 1, d, e, f, g; d = a&b;    e = a | c; f = a >> 1, g = a << 1; cout << “d= “ << d << “ e = “ << e << “ f = “ << f << “g = “ << g << endl; }
// 1. System.out.println("1."); int max = 5; for (int n = 1; n <= max; n++)...
// 1. System.out.println("1."); int max = 5; for (int n = 1; n <= max; n++) { System.out.println(n); } System.out.println(); // 2. System.out.println("2."); int total = 25; for (int number = 1; number <= (total / 2); number++) { total = total - number; System.out.println(total + " " + number); } System.out.println(); // 3. System.out.println("3."); for (int i = 1; i <= 2; i++) { for (int j = 1; j <= 3; j++) { for (int k = 1;...
QUESTION 1 The statement char ch = ‘A’ would store in ch A. The character A...
QUESTION 1 The statement char ch = ‘A’ would store in ch A. The character A B. ASCII value of A C. A along with the single inverted commas D. Both (a) and (b) 2 points    QUESTION 2 Which of the following scanf() statement will you use to accept following variables? float gravity_a; double gravity_b; A. scanf("%f %lf", &gravity_a, &gravity_b); B. scanf("%LF %f ", gravity_a, gravity_b); C. scanf("%LF %LF ", &gravity_a, &gravity_b); D. scanf("%f %LF ", gravity_a, gravity_b); 2...
1.) Answer the following programs a.) Let f be the following function: int f(char *s, char...
1.) Answer the following programs a.) Let f be the following function: int f(char *s, char *t) {     char *p1, *p2;     for(p1 = s, p2 = t; *p1 != '\0'&& *p2 != '\0'; p1++, p2++){               if (*p1 == *p2) break;     }     return p1 -s; } What is the return value of f(“accd”, “dacd”)? b.) What will be the output of the below program? #include <stdio.h> int main()   {    char x[]="ProgramDesign", y[]="ProgramDesign";   if(x==y){   printf("Strings are...
Write the MIPS assembly version of this C code: int weird(char[] s, int x) { int...
Write the MIPS assembly version of this C code: int weird(char[] s, int x) { int i; for (i = 0; i < x; i++) { if (s[i] == ‘A’) { return power(10, i); } } return -1; } int power(int base, int i) { int j = 0; while (j < base) { base = base * base; j++; } return base; }
int main(int argc, char *argv[]){ int fd1, fd2; char buffer[100]; long int n1; if(((fd1 = open(argv[1],...
int main(int argc, char *argv[]){ int fd1, fd2; char buffer[100]; long int n1; if(((fd1 = open(argv[1], O_RDONLY)) == -1) || ((fd2 = open(argv[2], O_CREAT|O_WRONLY|O_TRUNC,0700)) == -1)){ perror("file problem "); exit(1); } while((n1=read(fd1, buffer, 512) > 0)) if(write(fd2, buffer, n1) != n1){ perror("writing problem "); exit(3); } // Case of an error exit from the loop if(n1 == -1){ perror("Reading problem "); exit(2); } close(fd2); exit(0); } Could anyone fix the two issues in the while loop, so the ouput can...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha = 'A'; for(int i = 0; i < 13; i++){ for(int j = 0; j < 2; j++){ cout << alpha; alpha++; } } cout << endl; return 0; }
1)What is the output of the following code? struct someType { int a; int b; };...
1)What is the output of the following code? struct someType { int a; int b; }; void f1(someType &s) { s.a = 1; s.b = 2; } someType f2(someType s) { someType t; t = s; s.a = 3; return t; } int main() { someType s1, s2; f1(s1); s2 = f2(s1); cout << s1.a << '-' << s1.b << '-' << s2.a << '-' << s2.b << '-' << endl; return 0; } ------------------------------------------------------------------------------------------------------------------ 2) struct dateType { int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT