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

Array, String and I/O

Subject: Programming by using Java

1. Which of these operators is used to allocate memory to array variable in java?


a. malloc
b. alloc
c. new
d. new malloc
2. Which of these is an incorrect array declaration?
a. int arr[]=new int[5];
b. int []arr=new int[5];
c. int arr[]=new int[5];
d. int arr[]=int[5] new
3. What will be the output of the following Java code?
int arr[]=new int[5];
System.out.print(arr);
a. 0
b. Value stored in arr[0]
c. 00000
d. Class name@ hashcode in hexadecimal form
4. Which of these is an incorrect statement?
a. It is necessary to use new operator to initialize an array
b. Array can be initialized using comma separated expressions surrounded by curly braces
c. Array can be initialized when they are declared
d. None of the mentioned
5. What will be the output of the following Java Code?
public class Array
{
public static void main (String[] args)
{
int arr[]=new int[10];
for(int i=0;i<10;i--)
{
arr[i]=i;
System.out.println (arr[i]+" ");
i++;
}
}
}
a. 0 2 4 6 8
b. 1 3 5 7 9
c. 0 1 2 3 4 5 6 7 8 9
d. 1 2 3 4 5 6 7 8 9 10
6. In java arrays are ………………
a. Objects
b. Object references
c. Primitive data type

1
d. None of these
7. What is the result of compiling and running the following code?
public class Test
{
Public static void main(String []args)
{
int a[]=new int[0];
System.out.print(a.length);
}
}
a. 0
b. Compilation error, arrays cannot be initialized to zero size
c. Compilation error, it is a.length() not a.length
d. None of these
8. Array is a collection of ……………?
a. Same type of elements
b. Different type of elements
c. Heterogeneous
d. Both a and c
9. In java array are ………………
a. Objects
b. Object reference
c. Primitive data type
d. None of these
10. We access data in array using ………….
a. Operator
b. Variable
c. Index
d. Pointer
11. On the point of array initialized which is required to specify?
a. Row
b. Column
c. Rows and column
d. None of these
12. Which declaration is valid?
a. char [] arr=new char ();
b. char [] arr=new char[6];
c. char [] arr=new char(6);
d. char [] arr=new char[];
13. Array can allocate …………….
a. Static memory
b. Dynamic memory
c. Automatic
d. None of these
14. Which is wrong declaration of array?
a. int[] arr=new int[10];
b. int arr[]=new int[10];
c. int arr[]=new int[10];
d. int arr[]=int[10] new;
15. Index in array start with?

2
a. -1
b. 0
c. 1
d. Null
16. Which is used to declare, create, and initialize an array?
a. int arr[][]={1, 2, 3};
b. int arr[]=(1, 2, 3);
c. int arr[]={1, 2, 3};
d. int []arr=[];
17. We can determine the length of an array using …………...
a. sizeof(array);
b. array.len
c. array.legth
d. array.sizeof()
18. String in java is a?
a. Class
b. Object
c. Variable
d. Character array
19. Which of these methods of string class is used to obtain character at specified index?
a. char()
b. Charat()
c. Charat()
d. charAt()
20. Which od these keywords is used to refer to member of base class from a subclass?
a. upper
b. super
c. this
d. none of the mentioned
21. Which of these methods of String class can be used to test to strings for equality?
a. isequal()
b. isequals()
c. equal()
d. equals()
22. Which of the following statements are incorrect?
a. String is a class
b. Strings in java are mutable
c. Every string is an object of class string
d. Java defines a peer class of string, called StringBuffer, which allows string to be altered
23. What will be output of the following Java program?
public class String_demo
{
public static void main(String []args)
{
String obj = “I”+ “Like” + “Java”;
System.out.println(obj);
}
}
a. I
b. Like

3
c. Java
d. ILikeJava
24. What will be the output of the following program?
public class String_class
{
public static void main(String []args)
{
String obj = “I Love you”;
System.out.println(obj.charAt(3));
}
}
a. o
b. v
c. l
d. e
25. What is length of following string?
“I Like Java”
a. 9
b. 10
c. 11
d. 12
26. What will be output of the following Java program?
String obj= “Hello”;
String obj1= “World”;
String obj2=obj;
String obj2= “World”;
System.out.print(obj+ “ ”+obj2);
a. Hello Hello
b. World World
c. Hello World
d. World Hello
27. What will be output of the following Java program?
String obj= “Hello”;
String obj1= “World”;
String obj2= “Hello”;
System.out.print(obj.equals(obj1)+ “ ”+obj.equals(obj2));
a. false false
b. true true
c. true false
d. false trues
28. Which of these classes contains the methods used to write in a file?
a. FileStream
b. FileInputStream
c. BufferedOutputStream
d. FileBufferStream
29. Which of these exception is thrown in cases when the file specified for writing is not found?
a. IOException
b. FileException
c. FileNotFoundException
d. FileInputException

4
30. Which of these methods are used to read in from file?
a. get()
b. read()
c. scan()
d. readFileInput()
31. Which of these values is returned by read() method is end of file(EOF) is encountered?
a. 0
b. 1
c. -1
d. Null
32. Which of these exception is thrown by close() and read() methods?
a. IOException
b. FileException
c. FileNotFoundException
d. FileInputOutputException
33. Which of these methods is used to write into a file?
a. put()
b. putFile()
c. write()
d. writeFile()
34. What will be the output of the following Java Program?
import java.io.*;
public class filesinputoutput
{
public static void main (String[] args)throws IOException
{
FileInputStream obj=new FileInputStream("inputoutput.txt");
System.out.print (obj.available());
}
}
a. true
b. false
c. prints number of bytes in file
d. prints number of characters in file
35. Which of these class is used to read characters in a file?
a. FileReader
b. FileWriter
c. FileInputStream
d. InputStreamReader
36. Which of the following method is used to find length of the string?
a. strlen()
b. length()
c. sizeOf()
d. None of these
37. What will be output of the following program?
String s1= “Sangola”;
char x= s1.charAt(5);
System.out.println(x);
a. o
b. g

5
c. l
d. a
38. compareTo() method in string class method returns ………………. Type of value.
a. float
b. int
c. Boolean
d. String

You might also like