Midterm 1
Midterm 1
Midterm 1
Q1
1. What is the output of this code? 1 point
void main() {
var lInts = [1, 2, 3];
var lis = ['#0',for (var i in lInts)'#$i'];
assert(lis[1] == '#1');
}
void main() {
for (int i = 0; i < 10; i++) {
print(fibonacci(i));
}
}
int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
3. Function that ask the user for a number and determine whether the number
is prime or not. 1.5 point
Answer:
import 'dart:io';
void main() {
stdout.write('Enter a number: ');
int number = int.parse(stdin.readLineSync()!);
if (isPrime(number)) {
print('$number is a prime number');
1/6
Midterm 1
} else {
print('$number is not a prime number');
}
}
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= n ~/ 2; i++) {
if (n % i == 0) return false;
}
return true;
}
4. write code Flutter implements How to update text in Button when click? 3
point
Answer:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
2/6
Midterm 1
void _incrementCounter() {
setState(() {
_clickCount++;
});
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Clicked $_clickCount times',
style: TextStyle(fontSize: 24),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _incrementCounter,
child: Text('Click me!'),
),
],
);
}
}
1. What is Dart?
a. Dart is a object-oriented programming language
b. Dart is used to create a frontend user interfaces
c. Both of the above
d. None of the above
2. What will be the output of this program: void main() { int num;
print(num);}
a. Error
b. Null
3/6
Midterm 1
c. Num
d. None of these
4. Dart is an ...........
a. Open-source
b. Asynchronous
c. Programming language
d. All of the above
4/6
Midterm 1
10. Which of the following statements does not use string interpolation correctly?
a. print('Your name in upper case is $"me".toUpperCase');
b. print("Your name in upper case is ${'me'.toUpperCase()}");
c. print('Your name in upper case is ${"me".toUpperCase()}');
b. No
6/6