SECTION – A (15 x 1 = 15 MARKS)
Answer All Questions. Each question carries ONE Mark.
1. Choose the correct answer:
I) Java is example of:
A. High-level language B. Low-level language C. Machine language D. compiler
II) How can (10.99) be stored into int variable x using cast?
int x =__________;
A. 10.99 (double) B. (double) 10.99 C. 10.99 (int) D. (int) 10.99
III) _________ is a named location that stores a value.
A. Method B. String C. Variable D. Class
IV) ------------ is the position of an item inside an array.
A. Array B. index C. Variable D. Class
V) Array types look like other Java types except they are followed by
A. ( ) B. [ ] C. { } D. //
VI) sum - = discount is equivalent to_________
A. sum - -; B. sum = sum - discount; C. sum = discount; D. sum = discount - sum;
VII) Translating a program in a high-level language into a low-level language, all at once, in
preparation for later execution is called ……………..
A. Interpretation B. Compiling C. Parsing D. Debugging
VIII) One of the following is invalid (Wrong) variable type:
A. int B. string C. double D. boolean
IX) ……………. does NOT terminate the loop, it just skips some part of the loop.
A. Exit(0) B. Return C. main D. Continue
X) Class is a collection of related -----------------.
A. codes B. arguments C. methods D. Parameters
Comp-011 /programming-1 / 1441-42 /Semester (FALL) 1
XI) The …………… method must exist in any Java code to run.
A. public B. class C. main D. program
XII) The --------- statement will immediately end the program as soon as it is invoked
A. Exit(0) B. Return C. Break D. switch
XIII) The ----------- statements are necessary because without them, statements
in switch blocks fall through.
A. Exit(0) B. while C. Break D. Continue
XIV) A ---------- is a variable that stores an argument.
A. parameter B. class C. main D. program
XV) Statements are at least executed once even if the condition is false in __________ loop
A. for B. while C. do-while D. All of them
SECTION – B (5 x 3 = 15 MARKS)
Answer any FIVE Questions. Each Question carries THREE Marks.
2. Write a statement to check the number is positive or divisible by 3 ?
if( x < 0 || x % 3 == 0) (2 marks)
System.out .println(" x is positive or divisible by 3"); (0.5 marks)
else
System.out .println(" x is not positive or divisible by 3"); (0.5 mark)
3. What is the debugging, and write 2 kinds of errors in java?
The debugging is the process of tracking the errors down and correcting them. (1 mark)
— kinds of errors:
syntax – run time – logic (2 marks)
Comp-011 /programming-1 / 1441-42 /Semester (FALL) 2
4. Under line the errors then make the corrections:
public class Sum {
public static void main ( String [] args){
String name = Ali ; String name = "Ali" ; (1mark)
int age = 2.5 ; int age = 2 ; (1mark)
System.out.println(" my Name is " + name );
System (1mark)
system.out.println(" my Age is " + age ); }}
5. Complete the 6 missing parts in the following piece of code:
import java.util.Scanner; (0.5 mark)
public class Exercise{ (0.5 mark)
public static void main (String[] args){ (0.5 mark)
double[] numbers =new double[10]; (0.5 mark)
for (int i = 0; i < numbers.length; i++) { (0.5 mark)
numbers[i] =Math.random(); (0.5 mark)
System.out.println(numbers[i]);
}}}
6. What is the output of the following?
class Exercise { 1
public static void main(String[] args) {
2
3
int counter = 1; 4 (2.5 marks)
do { 5
System.out.println(counter);
if (counter %7 == 0) {
6
System.out.println("Multiple of 7 encountered. Quitting loop!"); 7
break;
} Multiple of 7 encountered.
counter++; } Quitting loop! (0.5 mark)
while (counter <= 10);
}}
7. What is the three jump statements in java?
1. Break (1 mark)
2. Continue (1 mark)
3. Return (1 mark)
Comp-011 /programming-1 / 1441-42 /Semester (FALL) 3
SECTION – C (2 x 5 = 10 MARKS)
Answer any TWO Questions. Each Question carries FIVE Marks.
8. - Write a Java program to find:
• The sum
• The average
Of the following array:
int[] numbers = {25, 14, 56, 15, 36, 56, 77, 18, 29, 49};
public class Exercise{ (0.5 mark)
public static void main(String[] args){ (0.5 mark)
int[] numbers = {25, 14, 56, 15, 36, 56, 77, 18, 29, 49}; (0.5 mark)
int sum = 0;
for(int i=0; i < numbers.length; i++) (1mark)
sum = sum + numbers[i]; (1mark)
double average = sum / numbers.length; (1mark)
System.out.println("Average value of the array elements is : " + average); }} (0.5mark)
9. Rewrite the following program by using do-while loop to print the even numbers
from (1 – 10):
public class Q9 { public class Q9 {
public static void main( public static void main(String[ ] args) { (0.5 mark)
int i = 1; (0.5 mark)
String[] args) { do { (1mark)
if(i%2 == 0) (0.5 mark)
for(int i = 1; i<=10; i++) {
System.out.print(i + " "); (0.5 mark)
if(i%2 == 0)
i++; (0.5 mark)
System.out.print(i + " "); }
} while(i <= 10); (1mark)
System.out.println(); } }
System.out.println( ); (0.5 mark)
}}
Comp-011 /programming-1 / 1441-42 /Semester (FALL) 4
10. Write a program in Java to print from 1 to 8 but Skip if it is divisible by 4 and
divisible by 2, using for Statement.
public static void main(String[] args) { (0.5 mark)
for(int i=1;i<=8;i++){ (1 mark)
if(i%4 == 0 && i%2 == 0){ (1.5 mark)
continue; (1 mark)
}
System.out.println(i); (1 mark)
}
}
}
Comp-011 /programming-1 / 1441-42 /Semester (FALL) 5