1. Select the correct option to write a program to find the sum of natural numbers upto 10.
#include
int main() {
int n, i, sum = 0;
printf("Enter a number upto which the natural numbers need to added: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
sum = i+1;
}
printf("Sum = %d", sum);
return 0;
}
#include
int main() {
int n, i;
printf("Enter a number upto which the natural numbers need to added: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
i++;
}
printf("Sum = %d", i);
return 0;
}
#include
int main() {
int n, i, sum = 0;
printf("Enter a number upto which the natural numbers need to added: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
printf("Sum = %d", i);
}
return 0;
}
#include
int main() {
int n, i, sum = 0;
printf("Enter a number upto which the natural numbers need to added: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
sum += i;
}
printf("Sum = %d", sum);
return 0;
}
2. Select the correct option to write a program to find whether a entered character is vowel or consonant.
#include
int main() {
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", &c);
lowercase_vowel.equals(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
uppercase_vowel.equals(c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}
#include
int main() {
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", &c);
lowercase_vowel== (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
uppercase_vowel== (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
if (lowercase_vowel == uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}
#include
int main() {
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", &c);
// evaluates to 1 if variable c is a lowercase vowel
lowercase_vowel.equals(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
// evaluates to 1 if variable c is a uppercase vowel
uppercase_vowel.equals(c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
// evaluates to 1 (true) if c is a vowel
if (lowercase_vowel && uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}
#include
int main() {
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", &c);
lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}
3. Select the correct option to write a program to find whether a number is a prime number
#include
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 2; i <= n % 2; ++i) {
// condition for non-prime
if (n % i == 0) {
flag = 1;
break;
}
}
if (n == 1) {
printf("1 is neither prime nor composite.");
}
else {
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
#include
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 2; i <= n / 2; ++i) {
// condition for non-prime
if (n % i == 2) {
break;
}
}
if (n == 1) {
printf("1 is neither prime nor composite.");
}
else {
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
#include
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 0; i <= n; ++i) {
// condition for non-prime
if (n % i == 0) {
flag = 1;
}
}
if (n == 1) {
printf("1 is neither prime nor composite.");
}
else {
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
#include
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 2; i <= n / 2; ++i) {
// condition for non-prime
if (n % i == 0) {
flag = 1;
break;
}
}
if (n == 1) {
printf("1 is neither prime nor composite.");
}
else {
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
4. Select the correct option to write a program to find whether the entered year is a leap year or not
#include
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
else {
printf("%d is not a leap year.", year);
}
return 0;
}
#include
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year / 400 == 0) {
printf("%d is a leap year.", year);
}
else if (year / 100 == 0) {
printf("%d is not a leap year.", year);
}
else if (year / 4 == 0) {
printf("%d is a leap year.", year);
}
else {
printf("%d is not a leap year.", year);
}
return 0;
}
#include
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
else {
printf("%d is not a leap year.", year);
}
return 0;
}
5. Select the correct option to write a program to swap two numbers without a third variable.
1. Select the correct option to rewrite without using ‘ if ’
if(bill < 7000)
discount = 0.05;
else
discount = 0.1;
discount = (bill < 7000)= 0.05 && 0.1;
discount = (bill < 7000)= 0.05 || 0.1;
discount = (bill < 7000)? 0.05 : 0.1;
2. Select the correct option to write a program to calculate the area of a triangle.
class ques4
{
public static void main(String args[])
{
int a = 2,b = 3,c = 4;
double area;
double s = (a+b+c)/2.0;
area = Math.area(s*(s-a)*(s-b)*(s-c))/(2*a*b);
System.out.println ("Area of the Triangle");
System.out.println ("of sides "+a+","+b+","+c);
System.out.println ("is : "+area);
}
}
class ques4
{
public static void main(String args[])
{
int a = 2,b = 3,c = 4;
double area;
double s = (a+b+c);
area = Math.area(s*(s-a)*(s-b)*(s-c))/(2*a*b);
System.out.println ("Area of the Triangle");
System.out.println ("of sides "+a+","+b+","+c);
System.out.println ("is : "+area);
}
}
class ques4
{
public static void main(String args[])
{
int a = 2,b = 3,c = 4;
double area;
double s = (a+b+c)/2.0;
area = Math.sqrt(s*(s-a)*(s-b)*(s-c))/(2*a*b);
System.out.println ("Area of the Triangle");
System.out.println ("of sides "+a+","+b+","+c);
System.out.println ("is : "+area);
}
}
3. What type of value is returned by Math.sqrt( )?
Double
4. Reverse a string in Java
public class StringPrograms {
public static void main(String[] args) {
String str = "123";
System.out.println(String.reverse(str));
}
}
public class StringPrograms {
public static void main(String[] args) {
String str = "123";
System.out.println(reverse(str));
}
public static String reverse(String in) {
if (in == null)
throw new IllegalArgumentException("Null is not valid input");
StringBuilder out = new StringBuilder();
char[] chars = in.toCharArray();
for (int i = chars.length - 1; i >= 0; i--)
out.append(chars[i]);
return out.toString();
}
}
5. Select the correct option to check vowels in a string
public class StringContainsVowels {
public static void main(String[] args) {
System.out.println(stringContainsVowels("Hello")); // true
System.out.println(stringContainsVowels("TV")); // false
}
public static boolean stringContainsVowels(String input) {
return input.toLowerCase() (".*[aeiou].*");
}
}
public class StringContainsVowels {
public static void main(String[] args) {
System.out.println(stringContainsVowels("Hello")); // true
System.out.println(stringContainsVowels("TV")); // false
}
public static boolean stringContainsVowels(String input) {
return input.contains (".*[aeiou].*");
}
}
public class StringContainsVowels {
public static void main(String[] args) {
System.out.println(stringContainsVowels("Hello")); // true
System.out.println(stringContainsVowels("TV")); // false
}
public static boolean stringContainsVowels(String input) {
return input.toLowerCase().matches(".*[aeiou].*");
}
}
1. Select the output of the following C++ code. Also, write the name of feature of Object Oriented Programming used in the following program jointly illustrated by the functions [I] to [IV]:
#include
void Lined //Function[I]
{
for(int L=1;L<=80;L++)
cout<<"-";
cout<
}
void Line(int N)
{
for(int L=1;L<=N;L++);
cout«"*";
cout<
}
void Line(char C,int N) //Function[III]
{
for(int L=1:L<=N:L++)
cout<
cout<
}
void Line(int M, int N)//Function[IV]
{
for(int L=1:L
cout<
cout<
}
void main()
{
int A=9, B=4, C=3;
char K=’#':
Line (K,B);
Line (A,C);
}
91822
90827
91826
Feature of C ++ Function overloading Output will be :
####
91827
2. What is the difference between double and float variables in Java?
In java, float takes 4 bytes in memory while Double takes 8 bytes in memory. Float is single precision floating point decimal number while Double is double precision decimal number.
In java, float takes 4 bytes in memory while Double takes 8 bytes in memory. Float is single precision floating point decimal number while Double is double precision decimal number.
3. Explain Method Overriding in Java
Method Overriding in Java allows a subclass to offer a specific implementation of a method that has already been provided by its parent or superclass. Method overriding happens if the subclass method and the Superclass method have:
4. Select the correct option to display current date and time?
import java.util.Date;
public class DateDemo {
public static void main(String args[]) {
// Instantiate a Date object
Date date = null;
System.out.println(date);
}
}
import java.util.Date;
public class DateDemo {
public static void main(String args[]) {
// Instantiate a Date object
Date date = null;
System.out.println(getDate());
}
}
import java.util.Date;
public class DateDemo {
public static void main(String args[]) {
// Instantiate a Date object
Date date = new Date();
// display time and date using toString()
System.out.println(date.toString());
}
}
5. What will be the exception produced by the following code snippet?
import java.io.*;
public class ExcepTest {
public static void main(String args[]) {
try {
int a[] = new int[2];
System.out.println("Access element three :" + a[3]);
} catch (Exception e) {
System.out.println("Exception thrown :" + e);
}
System.out.println("Out of the block");
}
}
Exception thrown :java.lang.ArrayIndexOutOfBoundsException: 3
Out of the block