Showing posts with label Apptitude Tests & MCQ Questions. Show all posts
Showing posts with label Apptitude Tests & MCQ Questions. Show all posts

Wednesday, September 30, 2020

Apptitude Tests MCQ Short Questions On Array In BlueJ

In this page you will get number of short questions - MCQ apptitude test questions on array. 

 Q 1. Which of these operators is used to allocate memory to array variable in Java?
A. malloc
B. alloc
C. new
D. new malloc

Ans: C

Q 2. What will this code print?

 int arr[] = new int [5];

 System.out.print(arr);

A. 0
B. value stored in arr[0].
C. 00000
D. Garbage value

Ans: D

Q 3.  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

Ans: A

Q 4. State the output
public void show()

{
    int arr[]=new int[5], i=0;
    while(i<5)
        arr[i]=++i;
    for(i=0; i<5; i++)
        System.out.print(" "+ arr[i]); 
}

A. 1 2 3 4 5
B. 0 1 2 3 4
C. Garbage Value 1 2 3 4
D. 1 2 3 4 Garbage Value

Ans: A

Q 5. State the output
public void show()
{
    int a[] = {5, 1, 15, 20, 25};
    int i, j, m;
    i = ++a[1];
    j = a[1]++;
    m = a[i++];
    System.out.print( i + ":"+ j +":" + m);  
}

A. 6:7:8
B. 4:5:6
C. 3:2:15
D. 2:3:4

Ans: C

Q 6.State the output
int a[] ={2,4,6,8,10};
a[0]=23;
a[3]=a[1];
int c= a[0]+a[1];
System.out.println("Sum = "+c);

A. Sum = 6
B. Sum = 27
C. Sum = 10
D. Sum = 31

Ans: B

Q 7.State the output
int a[4]={2,4,6,8};
for(i=0;i<=1;i++)
{
s=a[i]+a[3-i];
System.out.print(s+ " ");
}
A. 8 10
B. 10 10
C. 10 12
D. 8 12

Ans: B

Q 8. Difference between Subscript and Subscripted variable

Subscript is the index of the element in the array whereas Subscripted variable is the name of the array when it is used with a subscript to access a single element of the array.

Q 9. Difference between Ordinary variable and array variable

An ordinary variable can hold only one value whereas an array variable can refer to a group of values of the same data type by using a subscript.

Q 10. Difference between sorting and searching


Sorting

Searching

Sorting means to arrange the elements of the array in ascending or descending order.

Searching means to search for a term or value in an array.

Bubble sort and Selection sort are examples of sorting techniques.

Linear search and Binary search are examples of search techniques.


Q 11. Difference between Linear search and Binary search

Linear Search

Binary Search

Linear search works on sorted and unsorted arrays

Binary search works on only sorted arrays (ascending or descending)

Each element of the array is checked against the target value until the element is found or end of the array is reached

Array is successively divided into 2 halves and the target element is searched either in the first half or in the second half

Linear Search is slower

Binary Search is faster

 
Q 12. Selection sort and Bubble sort

Selection sort

Bubble sort

Selection Sort selects the smallest element from unsorted sub-array and swaps it with the leftmost unsorted element.

Bubble Sort compares adjacent elements and swaps them if they are in wrong order.

Performs lesser number of swaps to sort the same array relative to Bubble Sort

Performs more number of swaps to sort the array

Selection Sort is faster

Bubble Sort is slower


Q 13. length and length()

length

length()

length is an attribute i.e. a data member of array.

length() is a member method of String class.

It gives the length of an array i.e. the number of elements stored in an array.

It gives the number of characters present in a string.

Q. 14 What are the advantages of array

Ans: Using array we can store number of values of same time in same named space location. Also we can access any location value directly.

Q. 15 What are the limitations of array

Ans: There are many limitations of array. Array can store number of values but all are of same type. We can not store different type of values in a single array. An int type array can hold only int values.

Dynamic insertion and deletion of elements on array can be done but it is critical.

Q. 16 Why String is preferred over char type array

Both char type array and String are objects but array has only one data member - 'length' while String class has many methods and data members. Manipulation of String can be easily done through it's functions which is tough in case of char type array.


Q. 17 What are the advantages of index in array

There are many advantages of index in array. We can directly access any location value using the index. The array elements can be displayed from left to right or right to left using the index.


Q. 18 What are the different process of creating a numeric array


We can create an numeric array using the following techniques

1. int a[];

a=new int[10];

2. int a[]=new int[10];

3. int a[]={1,2,3,4,5};


Q. 19 State the relationship between array and loop


Loop and array are closely tied together. We need loop while assigning user input values in an array. We need loop to display array elements and any kind of searching and sorting of array elements, loop is essential.

 

Q. 20 If we pass an array to a function, is it call by value or call by reference?


If we use array as actual parameter or formal parameter, it is call by refernce.

Friday, September 25, 2020

Apptitude Tests MCQ Short Questions On Decision Making In BlueJ

 Q 1. Which of the following is not a decision making statement?
A. if-else
B. do-while
C. switch
D. if

Ans: B

Q 2. Which of the following is used with the switch statement?
A. break
B. do
C. Exit
D. Continue

Ans: A

Q 3. Give the output
public void show()
{
int a=20;
if(a<=0)
{
if(a==0)
{
System.out.print("Mera");
}
else
{
System.out.print("Bharat");
}
}
System.out.print("Mahan");
}

A. Mera
B.Bharat
C. Mahan
D.BharatMahan

Ans: C. The first condition (if(a<=0)) is false so the control skips the if body and executes System.out.print("Mahan");

Q 4. Give the output
public void show()
{
int a=4,b=5;
for(; b<10;b++)
{
 if(b%a==0)
{
 continue;
}
else if(b==10)
{
break;
}
 else
{
 System.out.print(b+ " ");
}
}
}

A. 5 6 7 9
B.1 2 3 4 5
C. 2 3 4 5
D.6 7 8 9

Ans: A. During first three iterations control goes to else body and the value of 'b' gets displayed. In the 4th iteration, control enters the if body (continue statement is encountered). Next value of 'b' is 9, so it is displayed and when 'b' becomes 10, condition of the loop is false.

Q 5:Give the output
        public void show()
        {
            int num1 = 10; 
            int num2 = 20;
            if ((num2 = 5) == num1)
            {
                System.out.print(num2);
            }
            else 
            {
                System.out.print(++num2);
            }
        } 
  
 A. 5
  B. 4
  C. 6
  D. 7

Ans: C num2 is initialised to 5. The conditional statement returns false and the else part gets executed.

Q 6:Give the output
Which of these statement is incorrect?
A. switch statement is more efficient than a set of nested if
B. switch statement can only test for equality, whereas if statement can evaluate any type of boolean expression
C. it is possible to create a nested switch statements
D. two case constants in the same switch can have identical values

Ans: D

Q 7: Which of these selection statements test only for equality?
A. switch
B. if & switch
C. if
D. None of these

Ans: A

Q 8: In a switch case, when the switch value does not respond to any case then the execution transfers to:

A. a break statement
B. a default case 
C. a loop
D. none

Ans: B

Q 9: Condition is essentially formed by using:

A. Arithmetic operators
B. Relational operators
C. Logical operators
D. All

Ans: D

Q 10:  If(a>b)&&(a>c)), then which of the statement is true?

A. b is the smallest number
B. b is the greatest number
C. a is the greatest number
D. all of the above

Ans: C

Q 12:
if(a>b)  
c=a;   
else   
c=b;
It can be written as:

A. c= (b>a)?a:b;
B. c= (a!=b)?a:b;
C. c= (a>b)?b:a;
D. None

And: D


Q 13:Which of the following statements accomplishes 'fall through'?
A. for statement
B. switch statement 
C. if-else
D. none

Ans: B

Q 14 A Java program executes but doesn't give the desired output.
It is due to:

A. the logical error in the program 
B. the syntax error in the program
C. the run time error in the program
D. none

Ans: A


Wednesday, September 23, 2020

Apptitude Tests MCQ Short Questions On Loop BlueJ


In this page you will find short questions on BlueJ loop. These questions are MCQ  apptitude tests questions.

1. class LoopMCQ
{
    public void show()
    {
        for(int i = 0; i <= 5; i++ )
        {
            System.out.println("i = " + i );
        }
        System.out.println("End of Loop = " + i );
    }
}


A. i = 0
   i = 1
   i = 2
   i = 3
   i = 4
   i = 5
   End of Loop = 6

B. i = 0
   i = 1
   i = 2
   i = 3
   i = 4
   i = 5
   i = 6

C. Compilation Errors

D. i = 0
   i = 1
   i = 2
   i = 3
   i = 4
   i = 5

    Correct Answer: C. 'i' is declared in the for loop so scope and lifetime of the variable is within that loop only.


2.class LoopMCQ
{

    public void show()
    {
        int i = 0;
        for(;i <= 5; i++ )
        {
            System.out.println("i = " + i );
        }

        System.out.println("End of Loop = " + i );
    }
}

A.   i = 0
      i = 1
      i = 2
      i = 3
      i = 4
      i = 5
     End of Loop = 6

B. i = 0
     i = 1
     i = 2
    i = 3
    i = 4
    i = 5
    i = 6

C. Compilation Errors

D. i = 0
     i = 1
     i = 2
     i = 3
     i = 4
     i = 5

 Correct Answer: A. 

3. class LoopMCQ
{

    public void show()
    {
        int a, b;
        for(a = 1, b = 4; a < b; a++, b--)
        {
            System.out.println("a = " + a);
            System.out.println("b = " + b);
        }
    }
}

A. Compilation error 

B. a = 1
     b = 4
     a = 2
     b = 3
     a = 3
     b = 2

C. a = 1
    b = 4
    a = 2
    b = 3

D. Run Time Error

Correct Answer: C. when 'a' becomes 3 and 'b' becomes 2, the loop terminates.

4. class LoopMCQ
{
    public void show()
{
    int i=0;
    for(;;)
    {
        if(i==10)
            break;
        System.out.print(++i);
    }
    
}
}

A. 0 1 2 3 4 5 6 7 8 9 10
B.  0 1 2 3 ... infinite times
C.  1 2 3 4 5 6 7 8 9 10
D.  1 2 3 4 5 6 7 8 9

Correct Answer: C. Prefix operator is used within System.out.print() method , so value of 'i' get incremented before display. Initial value of 'i' is 0, so the display starts from 1 and when the value of 'i' is 9, the loop displays 10 (prefix operator) and in the next iteration, loop terminates.

5. class LoopMCQ
{
    public void show()
{
    int i;
    for(i=0;i<10;++i)
    {
        System.out.print("#");
        if(i>6)
            continue;
        System.out.print(i);
    }
}   

A. #0#1#2#3#4#5#6###
B. #0#1#2#3#4#5#6#7#8#9#10
C. #0#1#2#3#4#5##7#8#9#10
D. #0#1#2#3#4#5#*/

Correct Answer: A. The loop displays both '#' and '0' until 'i' becomes 7. For the values 7,8 and 9 of 'i', only the '#' symbol is displayed as contunes statement sends the control to re-initialisation statement of the loop.

6. class LoopMCQ
{
    public void show()
{
    int i,j;
    char ch='A';
 
    for(i=5;i>=1;i--)
    {
        for(j=0;j< i;j++)
          System.out.print((char)(ch+j));
        System.out.println();
    }
}
}

1.  A B C D E
    A B C D E
    A B C D E
    A B C D E
   A B C D E
        
2.  A B C D
    A B C D
    A B C D
    A B C D
        
3.  A B C D
    A B C
    A B
    A
        
4.  A B C D E
    A B C D
    A B C
    A B
    A

 Correct Answer: 4. Outer loop executes for 5 times and on each iteration of outer loop, one new line is feed. So, answers may be 1 or 4. Inner loop's iteration is gradually decreased with the iteration of outer loop. So the answer is 4.


7. class LoopMCQ
{
    public void show()
{
    int i=1;
    while(i>=10)
    {
        System.out.print(i);
        i+=1;
    }
            System.out.print("End of Loop"+i);
           System.out.println();
}
}

A. After loop i=1
B. 1,
C. After loop i=2
D. After loop i=2

Correct Answer: A. Loop will not be executed for a single time. Only display and that is from outside the loop.

8. class LoopMCQ
{
public void show()
{
int i = 1, j = 1;
for(--i , j++ ; i<10; i+=2)
{
System.out.print(i+" ");
}
}
}

A. Compilation error

B. Program never ends

C. 0 2 4 6 8

D. None of the above

Correct Answer: C.  'i' starts the loop with value 0 and gradually increases by step 2.

Output of following


1. for(i=1;i<=5;i++)
{
 System.out.println(i+ ++i);
}

12
34
56

2. int a=2,i
for(i=8;i>=1;i--)
{
 System.out.println(i+a);
i-=a
}
82
52
22

3. for(i=1;;i+=2)
{
if(i<=6)
 System.out.println(i*2);
else
break;
}

2
6
10


4. for(i=2;i<=8;i*=2)
System.out.println(i);
System.out.println(i*2);

2
4
8
32

5. for(char a=’A’=2;a<=’E’;a++)
System.out.println((char)a+1);

B
C
D
E
F


6. for(i=10;i>=1;i--)
{

System.out.println(i);
if(i<6)
break
}
10
9
8
7
6
5


7. for(i=1;i<=10;i++)
{

if(i==7)
continue;
else
System.out.print(i);
}

1 2 3 4 5 6 8 9 10


8. for(i=10;;i--)
{

if(i==0)
break;
System.out.print(i+ “ “);
}

10  9  8  7  6  5  4  3  2  1


Wednesday, August 5, 2020

Apptitude Tests MCQ Short Questions On Increment and Decrement Operators BlueJ

Increment and Decrement Operators


Give the output:

1. a+=++a-b-- + a+b where a=2 and b=6

a=a+ ++a-b-- + a+b
=2+3-6+3+5
=5-6+3+5
=13-6
=7


2. x=x++ + (++x)%5 where x=10

10+12%5
10+2
=12

3. m*=m+n++ + ++m where m=20 and n=15

m*=20+15 + 21
m*= 56
m=m*56
m=20*56
m=1120

4. q=(++q/p-- + q++)*p/4 where p=75 and q=90

(91/75+91)*74/4
(1+91)*74/4
=92*74/4
=6808/4
=1702

5. a+=--a + b*3 + c++ - ++b where a=2, b=3 and c=10

a+=1+3*3 + 10-4
a+=1+9+6
a+=16
a=a+16
a=2+16
a=18

6. z=(++y*(y++ + 5)) where z=10

=11*(11+6)
=11*17

7. a+=a++ + ++a + --a + a-- where a=7 

 a+=7+9+8+8
 a+=32
a=a+32
a=7+32
a=39

8. int i=0;
while(++i<10)
{
 System.out.print(" "+i);
}

Output: 1 2 3 4 5 6 7 8 9


9. int i=0;
while(i++<10)
{
  System.out.print(" "+i);
}

Output: 1 2 3 4 5 6 7 8 9 10

10. int i=0,j=10;
for(;++i<j-- )
{
System.out.print(i + “:”+j);
}

Output:
1 9
2 8
3 7
4 6

11. int a=10,b=5;
for(int i=0;i<10;i++)
{
 int c=a++ + --b;
System.out.print( c + “ “);
}

Output: 
14 14 14 14 14 14 14 14 14 14

12. a=16, b=-1

int a=5,b=6,c=7;
a*=a++ + ++a + b-- * ++c;
  =5 + 7+6*8
 =5+7+48
=60
a=a*60
    =5*60
=300


13. int a=10, b=11,c=12;
int d=a++ * a++ + ++b/c++ + c++;
=10*11+1+13
=110+14
=124

14. x=10
x=x++ + ++x%5
=10+12%5
=10+2
=12

15. int a=2,b=6;
a+=++a – b-- + a+b
      =3-6+3+5
=5
a=a+5
a=2+5
=7

16. int n=15, m=20
m*=m+ n++ + ++m;
       =20+15+21
      =56
m=m*56
m=20*56
=1120

17. int p=75, q=90
q=(++q/p--+q++)*p/4
=(91/75+91)*74/4
(1+91)*74/4
92*74/4
23*74

18. y=10
z=(++y*(y++ + 5))
   = 11*(11+5)
    =11*16
 = 176

19. a+=a++ + ++a + --a+a—
a=7
7+9+8+8
=32
a=a+32
a=7+32
=39

20. a=2,b=3,c=10
a+=--a+b*3+c++ - ++b;
       =1+9+10-4
=16
a=a+16
=2+16
=18


Subscribe via email

Enter your email address:

Delivered by FeedBurner