Question 01

Consider the following Dart code:

        void main() 
        {
            int a = 5;
            int b = 3;
            int c = a + b * 2;
        }

What is the final value of the variable c?

  • 11

  • 16

  • 13

  • 10

Answer 01

The multiplication has a higher precedence than addition, so b * 2 is computed first, which gives 6. Then a + 6 gives 11. Therefore, the final value of c is 11.

Question 02

Consider the following Dart code:

        void main() 
        {
            int x = 10;
            int y = 4;
            int result;
            
            if (x % y == 0) 
            {
                result = x ~/ y;
            } 
            else 
            {
                result = x % y;
            }
        }

What is the final value of the variable result?

  • 2

  • 4

  • 1

  • 10

Answer 02

The condition x % y == 0 is false because 10 % 4 gives 2, not 0. Therefore, the else branch is executed, and the value of result becomes 10 % 4, which is 2. Hence, the final value of result is 2.

Question 03

Consider the following Dart code:

        void main() 
        {
            int sum = 0;
            for (int i = 1; i <= 5; i++) 
            {
                if (i % 2 == 0) 
                {
                    sum += i;
                }
            }
        }

What is the final value of the variable sum?

  • 5

  • 6

  • 10

  • 15

Answer 03

The loop runs from i = 1 to i = 5. The if condition checks if i % 2 == 0, i.e., whether i is even. When i = 2 and i = 4, the condition is true, so sum is updated to 0 + 2 = 2, then to 2 + 4 = 6. The final value of sum is 6.

Question 04

Consider the following Dart code:

        void main() {
            int a = 8;
            int b = 3;
            int result = 0;
            
            if (a - b > 5) 
            {
                result = a * b;
            } 
            else 
            {
                result = a + b;
            }
            
            result = result ~/ 2;
        }

What is the final value of the variable result?

  • 4

  • 12

  • 20

  • 5

Answer 04

The condition a - b > 5 is true because 8 - 3 = 5, which is not greater than 5. Therefore, the else branch is executed, and result becomes a + b, i.e., 8 + 3 = 11. Afterward, result is updated to 11  / 2, which performs integer division, giving 5. Hence, the final value of result is 5.

Question 05

Consider the following Dart code:

        void main() 
        {
            List<int> numbers = [1, 2, 3, 4, 5];
            int total = 0;
            
            for (int i = 0; i < numbers.length; i++) 
            {
                if (numbers[i] % 2 != 0) 
                {
                    total += numbers[i];
                }
            }
        }

What is the final value of the variable total?

  • 6

  • 9

  • 12

  • 15

Answer 05

The loop iterates over the list numbers. It checks each number: 1, 2, 3, 4, 5. The condition numbers[i] % 2 != 0 is true for 1, 3, and 5. Thus, total is updated as follows: - For 1: total = 0 + 1 = 1 - For 3: total = 1 + 3 = 4 - For 5: total = 4 + 5 = 9

Hence, the final value of total is 9.

Last modified: Tuesday, 29 October 2024, 9:02 PM