Question 1
The function f is defined as follows:
int f(int n) {
if (n <= 1) return 1;
else if (n % 2 == 0) return f(n / 2);
else return f(3 * n - 1);
}
int f (int n) {
if (n <= 1) return 1;
else if (n % 2 == 0) return f(n/2);
else return f(3n - 1);
}
int f(int n) {
if (n <= 1) return 1;
else if (n % 2 == 0) return f(n / 2);
else return f(3 * n - 1);
}
def f(n):
if n <= 1:
return 1
elif n % 2 == 0:
return f(n // 2)
else:
return f(3 * n - 1)
function f(n) {
if (n <= 1) return 1;
else if (n % 2 === 0) return f(n / 2);
else return f(3 * n - 1);
}
Assuming that arbitrarily large integers can be passed as a parameter to the function, consider the following statements.
1. The function f terminates for finitely many different values of n ≥ 1.
ii. The function f terminates for infinitely many different values of n ≥ 1.
iii. The function f does not terminate for finitely many different values of n ≥ 1.
iv. The function f does not terminate for infinitely many different values of n ≥ 1.
Which one of the following options is true of the above?
(i) and (iii)
(i) and (iv)
(ii) and (iii)
(ii) and (iv)
Question 2
class Test
{
public static void main(String[] args)
{
String str = "geeks";
str.toUpperCase();
str += "forgeeks";
String string = str.substring(2,13);
string = string + str.charAt(4);;
System.out.println(string);
}
}
Question 3
class Test {
public static void swap(Integer i, Integer j) {
Integer temp = new Integer(i);
i = j;
j = temp;
}
public static void main(String[] args) {
Integer i = new Integer(10);
Integer j = new Integer(20);
swap(i, j);
System.out.println("i = " + i + ", j = " + j);
}
}
Question 4
class Test
{
public void demo(String str)
{
String[] arr = str.split(";");
for (String s : arr)
{
System.out.println(s);
}
}
public static void main(String[] args)
{
char array[] = {'a', 'b', ' ', 'c', 'd', ';', 'e', 'f', ' ',
'g', 'h', ';', 'i', 'j', ' ', 'k', 'l'};
String str = new String(array);
Test obj = new Test();
obj.demo(str);
}
}
ab cd ef gh ij kl
ab cd;ef gh;ij kl
Question 5
program side-effect (input, output); var x, result: integer; function f (var x:integer):integer; begin x:x+1;f:=x; end; begin x:=5; result:=f(x)*f(x); writeln(result); end;
Question 6
Consider the following Java code fragment:
1 public class While
2 {
3 public void loop()
4 {
5 int x = 0;
6 while(1)
7 {
8 System.out.println("x plus one is" +(x+1));
9 }
10 }
11 }
There is syntax error in line no. 1
There is syntax errors in line nos. 1 & 6
There is syntax error in line no. 8
There is syntax error in line no. 6
Question 7
Study the following program:
// precondition: x>=0
public void demo(int x)
{
System.out.print(x % 10);
if (x % 10 != 0) {
demo(x / 10);
}
System.out.print(x % 10);
}
Which of the following is printed as a result of the call demo(1234)?
1441
3443
12344321
4321001234
Question 8
Consider the following JAVA program :
public class First {
public static int CBSE (int x) {
if (x < 100) x = CBSE (x + 10);
return (x – 1);
}
public static void main (String[] args){
System.out.print(First.CBSE(60));
}
}
What does this program print ?
59
95
69
99
Question 9
int [ ] p = new int [10]; int [ ] q = new int [10]; for (int k = 0; k < 10; k ++) p[k] = array [k]; q = p; p[4] = 20; System.out.println(array [4] + “ : ” + q[4]);
Question 10
class Main {
public static void main(String args[]) {
System.out.println(fun());
}
int fun()
{
return 20;
}
}
There are 17 questions to complete.