Java Sorular 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1

Java Questions
Starlang

1. Which of the following can not be a variable if (grade <= 50){


name? System.out.println( ”F” );
}
a) 1sum
b) s1 b)
c) integer if (grade > 70){
d) sum1 System.out.println( ”AA”);
}
e) toplam if (grade > 50){
2. Given T as an integer, how do you check if T is System.out.println( ”BB”);
} else {
not between 0 and 10?
System.out.println( ”F” );
a) if (not (0 < T < 10)) }
b) if (T > 0 || T < 10) c)
c) if (T < 0 || T > 10) if (grade > 70){
d) if (T < 0 && T > 10) System.out.println( ”AA”);
e) if (T > 0 && T < 10) } else {
if (grade < 50){
3. What is the value of integer variable x after the System.out.println( ”BB”);
execution of the following line? } else {
System.out.println( ”F” );
int x = 3 / 2 + 3 / 2 + 6 / 4 + 6 / 4;
}
}
a) 0
b) 8 d)
c) 2 if (grade > 70){
System.out.println( ”AA”);
d) 4
} else {
e) 6 if (grade > 50){
4. Which of the following expressions result in value System.out.println( ”BB”);
} else {
1? System.out.println( ”F” );
a) 2 % 1 }
b) 15 % 4 }
c) 25 % 5 e)
d) 38 % 6 if (grade > 70){
e) 16 % 3 System.out.println( ”AA”);
}
5. Which of the following code fragments prints if (grade > 50){
”AA” if the grade is larger than 70, ”BB” if the System.out.println( ”BB”);
grade is less than 70 and larger than 50, ”F” }
otherwise. if (grade <= 50 && grade < 70){
a) System.out.println( ”F” );
if (grade > 70){ }
System.out.println( ”AA”);
}
if (grade > 50){
System.out.println( ”BB”);
}
2

6. Suppose that x is a user given 3 digit integer break;


(e.g. 635), which of the following prints its hundreds }
digit (e.g. 6)? d)
a) System.out.println (x ∗ 100); switch (x){
b) System.out.println (x / 100); case 4:
c) System.out.println (( x / 100) / 100); case 9: System.out.println( ”Done”);
case 1:
d) System.out.println (( x % 100) % 100);
case 2:
e) System.out.println (x % 100); case 3: System.out.println( ”Not done”);
break;
7. Which of the following defines a variable of type
}
boolean?
e)
a) boolean b1 = 0;
switch (x){
b) boolean b2 = ’false’;
case 4:
c) boolean b3 = false; case 9: System.out.println( ”Done”);
d) boolean b4 = Boolean.false(); break;
e) boolean b5 = no; case 1:
case 2:
8. Which of the following codes is the same as the case 3: System.out.println( ”Not done”);
following code? default: System.out.println( ” Invalid ” );
}
if (x == 4 || x == 9)
System.out.println ( ”Done”);
else 9. What is the output of the following code, if the
if (x >= 1 && x <= 3) user enters 0?
System.out.println( ”Not done”);
else int choice = input . nextInt ();
System.out.println( ” Invalid ” ); int j = 0;
switch (choice){
a) case 0:
switch (x){ j += 1;
case 4: case 1:
case 9: System.out.println( ”Done”); j += 2;
case 1: case 2:
case 2: j += 3;
case 3: System.out.println( ”Not done”); case 3:
} j += 4;
b) default:
j += 5;
switch (x){
}
case 4:
System.out.println( j );
case 9: System.out.println( ”Done”);
break;
case 1:
a) 1
case 2: b) 3
case 3: System.out.println( ”Not done”); c) 6
break; d) 10
default: System.out.println ( ” Invalid ” ); e) 15
}
10. Which of the following is the correct way of
c) defining an array?
switch (x){ a) int [] myList = {’1’, ’2’, ’3’};
case 4:
case 9: System.out.println( ”Done”);
b) int [] myList = (5, 8, 2);
break; c) int myList [] [] = {4, 9, 7, 0};
case 1: d) int [] myList = {4, 3, 7};
case 2: e) int myList = {2, 9, 0};
case 3: System.out.println( ”Not done”);
3

11. What is the output of the following code? a) {10, 20, 30, 40}
int sum = 0;
b) {40, 10, 10, 10}
int i = 0; c) {40, 40, 40, 10}
while ( i < 5) { d) {10, 40, 40, 40}
sum = sum + i; e) {10, 10, 10, 10}
i ++;
} 15. What will be the output of the following pro-
System.out.println( i + ” ” + sum); gram?

a) 5 10 int sum = 0;
for ( int i = 1; i <= 10; i++){
b) 6 10 for ( int j = i ; j <= 10 − i; j ++){
c) 4 10 sum++;
d) 5 11 }
e) 6 11 }
System.out.println(sum);
12. Which of the following is the correct definiton
of a method? a) 55
1) public static void method() {} b) 110
2) public static void method(void) {} c) 15
3) public static method() {} d) 25
4) public static method(void) {} e) 10
a) 1, 2
16. Consider the following for loop
b) 1
c) 1, 2, 3 for (A; B; C){
d) 2, 4 }
e) 1, 2, 3, 4
How do you convert this for loop into a while loop?
a)
13. What will be the output of the following pro-
gram? A;
while (B){
int sum = 0; }
for ( int i = 1; i <= 5; i ++){ C;
for ( int j = 1; j <= 5; j++){ b)
for ( int k = 1; k <= 5; k++){
sum++; B;
} while (A){
} C;
} }
System.out.println(sum); c)
A;
a) 55 while (C){
b) 35 B;
c) 210 }
d) 50 d)
e) 125 A;
B;
14. What is the content of the array list after while (C){
executing the following code? }
int [] list = {10, 20, 30, 40}; e)
int tmp = list [ list .length − 1]; A;
for ( int i = 1; i < list .length; i ++) { while (B){
list [ i ] = list [ i − 1]; C;
} }
list [0] = tmp;
4

17. What will be the output of the following pro- d)


gram? for ( int i = 1; i <= 4; i++)
printStar ( i );
for ( int i = 1; i <= 3; i ++){
for ( int i = 1; i <= 3; i++)
for ( int j = 1; j <= 3; j++){
printStar ( i );
System.out.print(”∗” );
} e)
System.out.print(”∗” ); for ( int i = 1; i <= 4; i++)
System.out.println (); printStar ( i );
} for ( int i = 3; i >= 1; i−−)
printStar ( i );
a) ****
**** 19. Let say you have the functions factorial and
**** power
b) ***
*** public static int factorial ( int N)
public static int power(int x, int y)
***
c) ***
which calculate N ! and xy respectively. Which of
** the following code fragments then calculate the
* following sum:
d) *
** 21 22 2N
+ + ... +
*** 1! 2! N!
e) ********* a)
double sum = power(2, N) / factorial (N);
18. Let say you have the following function defined:
b)
public static void printStar ( int n){ double sum = 0;
for ( int i = 1; i <= n; i++){ for ( int i = 1; i <= N; i++){
System.out.print(”∗”) sum += power(2, N) / ( factorial ( i ) + 0.0);
} }
System.out.println ();
} c)
double sum = 0;
Which of the following code fragments produce the for ( int i = 1; i <= N; i++){
following output? sum += power(2, i) / ( factorial ( i ) + 0.0);
}

∗∗ d)
∗∗∗ double sum = 0;
∗∗∗∗ for ( int i = 1; i <= N; i++){
∗∗∗ sum += power(2, i) / factorial ( i );
∗∗ }
∗ e)
a) double sum = 0;
for ( int i = 1; i <= 4; i++) for ( int i = 1; i <= N; i++){
printStar (1); sum += power(2, i) / ( factorial (N) + 0.0);
b) }
for ( int i = 1; i <= 4; i++)
printStar ( i );
c)
for ( int i = 1; i <= 4; i++)
printStar (1);
for ( int i = 1; i <= 3; i++)
printStar (1);
5

20. Let say you have the function d)


public static int sumOfDivisorsExceptItself(int N) public static int [] createArray(){
int [] array;
which returns the sum of divisors of N except itself. array = new int[100];
for ( int i = 0; i < 100; i++)
Which of the following code fragments prints all array[ i ] = 1;
perfect numbers until 1000, where a number is return array;
perfect if it is equal to the sum of its divisors? }
a)
System.out.println (sumOfDivisorsExceptItself(100));
e)
public static int createArray(){
b) int [] array;
for ( int i = 2; i <= 1000; i++){ array = new int[100];
System.out.println(sumOfDivisorsExceptItself(i )); for ( int i = 0; i < 100; i++)
} array[ i ] = 1;
c) return array;
}
for ( int i = 2; i <= 1000; i++){
if (sumOfDivisorsExceptItself(i)){
System.out.println ( i ); 22. Which of the following functions takes a param-
} eter of an array, and returns the sum of elements of
}
that array?
d) a)
for ( int i = 2; i <= 1000; i++){ public static int sumArray(int[] array){
System.out.println( i ); sum(array);
} }
e) b)
for ( int i = 2; i <= 1000; i++){ public static int sumArray(int[] array){
if (sumOfDivisorsExceptItself(i) = i ){ int sum = 0;
System.out.println ( i ); for ( int i = 0; i < array.length; i ++)
} sum += array[i ];
} }
c)
21. Which of the following functions creates and public static int [] sumArray(int[] array){
returns an array of 100 integers, where each element int sum = 0;
for ( int i = 0; i < array.length; i ++)
of the array is 1.
a) sum += array[i ];
return sum;
public static int createArray(){
}
return 1;
} d)
b) public static int sumArray(int[] array){
int sum = 0;
public static int createArray(){
for ( int i = 0; i < array.length; i ++){
int [] array;
array[ i ] = input . nextInt ();
for ( int i = 0; i < 100; i++)
sum += array[i ];
array[ i ] = 1;
}
return array;
return sum;
}
}
c)
e)
public static int [] createArray(){
public static int sumArray(int[] array){
int [] array;
int sum = 0;
for ( int i = 0; i < 100; i++)
for ( int i = 0; i < array.length; i ++)
array[ i ] = 1;
sum += array[i ];
return array;
return sum;
}
}
6

23. Which of the following can be used to get the e)


number of elements of an array? public static int smaller(int [] array){
a) arr.length int count = 0;
b) arr.length-1 for ( int i = 0; i < array.length; i ++){
if (array[ i ] < N){
c) arr.size
count++;
d) arr.length() }
e) arr.size() }
return count;
24. Which of the following functions takes a param- }
eter of an array and a number N, and returns the
number of elements of that array that are smaller
25. Which of the following functions takes a param-
than N?
a) eter of an array and a number N, and returns true
public static int smaller(int [] array, int N){ ifa)the array contains N and false otherwise?
N = intput . nextInt ();
public static boolean contains(int[] array, int N){
int count = 0;
array.contains(N);
for ( int i = 0; i < array.length; i ++){
}
if (array[ i ] < N){
count++; b)
} public static boolean contains(int[] array, int N){
} for ( int i = 0; i < array.length; i ++){
return count; if (array[ i ] == N){
} return true;
b) else
return false;
public static int smaller(int [] array, int N){
}
int count;
}
for ( int i = 0; i < array.length; i ++){
c)
if (array[ i ] < N){
count++; public static boolean contains(int[] array, int N){
} for ( int i = 0; i < array.length; i ++){
} if (array[ i ] == N){
return count; return true;
} }
c) }
return false;
public static int smaller(int [] array, int N){ }
int count = 0; d)
for ( int i = 0; i < array.length; i ++){
if (array[ i ] < N){ public static boolean contains(int[] array, int N){
count++; for ( int i = 0; i < array.length; i ++){
} if (array[ i ] == N){
} return false;
return count; else
} return true;
}
d) }
public static int smaller(int [] array, int N){ e)
int count = 0;
public static boolean contains(int[] array, int N){
for ( int i = 0; i < array.length; i ++){
N = input. nextInt ();
if (array[ i ] < N){
for ( int i = 0; i < array.length; i ++){
count++;
if (array[ i ] == N){
} else {
return true;
return count;
}
}
}
}
return false;
return count;
}
}

You might also like