[go: up one dir, main page]

0% found this document useful (0 votes)
86 views9 pages

1.tushar Assignment 1

1. The document provides examples of code snippets and questions to test understanding of basic programming concepts like operators, expressions, statements, blocks, etc. 2. It includes exercises to check if a number is even or odd, positive or negative, a leap year, and if a character is a vowel or consonant by accepting user input. 3. Another exercise demonstrates calculating the sum of first n natural numbers divisible by 3 or 5 using a method with an integer argument and return type.

Uploaded by

Tushar Jaiswal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views9 pages

1.tushar Assignment 1

1. The document provides examples of code snippets and questions to test understanding of basic programming concepts like operators, expressions, statements, blocks, etc. 2. It includes exercises to check if a number is even or odd, positive or negative, a leap year, and if a character is a vowel or consonant by accepting user input. 3. Another exercise demonstrates calculating the sum of first n natural numbers divisible by 3 or 5 using a method with an integer argument and return type.

Uploaded by

Tushar Jaiswal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Assignment 1 (Basics)

1. Consider the following code snippet.

int i = 10;
int n = i++%5;

a. What are the values of i and n after the code is executed?

Answer:i =11 and n = 0.
b. What are the final values of i and n if instead of using the postfix increment operator (i++), you
use the prefix version (++i))?

Answer:i =11 and n = 1.

2. To invert the value of a boolean, which operator would you use?


Answer:The logical complement operator "!".

3. Which operator is used to compare two values, = or == ?


Answer:The equality operator (==).

4. Explain the following code sample: result = someCondition ? value1 : value2;

Answer:"If someCondition is true , assign the value of value1 to result . Otherwise, assign the value of
value2 to result ."

Expression

Expressions, Statements, and Blocks


Questions
1. Operators may be used in building ___, which compute values.

Answer: expressions.

2. Expressions are the core components of ___.


Answer:Statements.

3. Statements may be grouped into _"Blocks"__.

4. The following code snippet is an example of a _"Compound"__ expression.

1 * 2 * 3

5. Statements are roughly equivalent to sentences in natural languages, but instead of ending with a
period, a statement ends with a __"semicolon"_.
6. A block is a group of zero or more statements between balanced _"braces"__ and can be used
anywhere a single statement is allowed.
Exercises
Identify the following kinds of expression statements:

● aValue = 8933.234;

Answer:assignment statement

● aValue++;

Answer: increment statement

● System.out.println("Hello World!");

Answer: method invocation statement

● Bicycle myBike = new Bicycle(); Answer:object creation statement

Questions :

1. Accept a number from user and check whether it is even or odd


Answer:
package Tushar;
/*Java Program to check whether a number is even or odd*/
import java.util.Scanner;
public class odd_even {
public static void main(String args[])
{
//Accept a Number from the user
//Create an object of scanner class
Scanner input = new Scanner(System.in);
int num; //Declare a variable
System.out.println("Enter a number:");
num = input.nextInt();
//If number is divisible by 2 then it's an even
number
//else odd number
if ( num % 2 == 0 )
System.out.println("The entered number is
even");
else
System.out.println("The entered number is odd");
} }
2. Accept a number from user and check whether it is positive or
negative
Answer:
package Tushar;
import java.util.Scanner;
public class positive_negative {
public static void main(String[] args)
{
int number;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number you want to
check:");
number = scan.nextInt();
scan.close();
if(number > 0)
{
System.out.println(number+" is positive
number");
}
else if(number < 0)
{
System.out.println(number+" is negative
number");
}
else
{
System.out.println(number+" is neither
positive nor negative");
}
}
}
3. Accept a year from user and check whether it is leap year or not.
Answer:
package Tushar;
import java.util.Scanner;
public class leap_year {
public static void main(String[] args){
int year;
System.out.println("Enter an Year :: ");
Scanner sc = new Scanner(System.in);
year = sc.nextInt();
if (((year % 4 == 0) && (year % 100!= 0)) || (year
%400 == 0))
System.out.println("Specified year is a leap
year");
else
System.out.println("Specified year is not a
leap year");
}
}

4. Accept a character from user and check whether it is vowel or


consonant.
Answer: package Tushar;
import java.util.Scanner;
public class vowel {
public static void main(String args[]){
System.out.println("Enter a character :");
Scanner sc = new Scanner(System.in);
char ch = sc.next().charAt(0);
if(ch == 'a'|| ch == 'e'|| ch == 'i' ||ch == 'o'
||ch == 'u'||ch == ' '){
System.out.println("Given character is an
vowel");
}else{
System.out.println("Given character is a
consonant");
}
}}
Questions
1. Real-world objects contain "state" and "behavior".

2. A software object's state is stored in "fields".

3. A software object's behavior is exposed through "methods".

4. Hiding internal data from the outside world, and accessing it only through publicly exposed methods is
known as data "encapsulation".

5. A blueprint for a software object is called a "class".

6. Common behavior can be defined in a "superclass" and inherited into a "subclass" using the "extends"
keyword.

7. A collection of methods with no implementation is called an "interface".

8. A namespace that organizes classes and interfaces by functionality is called a "package".

9. The term API stands for "Application Programming Interface".


Exercise 5: Create a class with a method which can calculate the sum of first n natural numbers which
are divisible by 3 or 5.

Method Name calculateSum

Method Description Calculate Sum

Argument int n

Return Type int-sum

Logic Calculate the sum of first n natural numbers which


are divisible by 3 or 5.

Answer:package Tushar;
public class calculate_sum {
// Function to calculate the sum
// of numbers divisible by 3 or 5
static int sum(int N, int X, int Y)
{
int S1, S2, S3;
S1 = ((N / X)) * (2 * X + (N / X - 1) * X)
/ 2;
S2 = ((N / Y)) * (2 * Y + (N / Y - 1) *
Y) / 2;
S3 = ((N / (X * Y))) * (2 * (X * Y)
+ (N / (X * Y) - 1) * (X * Y))/
2;
return S1 + S2 - S3;
}
public static void main(String
[]args)
{
int N = 10;
int X = 3, Y = 5;
System.out.println(sum(N, X, Y));
}
}
Output is: 33
Exercise 6: Create a class with a method to find the difference between the sum of the squares and the
square of the sum of the first n natural numbers.

Method Name calculateDifference

Method Description Calculate the difference

Argument int n

Return Type int - Sum

Logic Find the difference between the sum of the


squares of the first n natural numbers and the
square of their sum.

For Example if n is 10,you have to find

(1^2+2^2+3^2+….9^2+10^2)-

(1+2+3+4+5…+9+10)^2

Answer:

package Tushar;
public class difference {
static int square_diff(int n ) {
int l,k, m;
l = (n * (n + 1) * (2 * n + 1)) / 6;
k = (n * (n + 1)) / 2;
k = k * k;
m =
Math.abs(l - k);
return m;
}
public static void main(String s[])
{
int n = 10;
System.out.println(square_diff(n)); }
}

Output:2640

Exercise 7: Create a method to check if a number is an increasing number


Method Name checkNumber

Method Description Check if a number is an increasing number

Argument int number

Return Type Boolean

Logic A number is said to be an increasing number if


no digit is exceeded by the digit to its left.

For Example : 134468 is an increasing number

package Tushar;
Answer:
import java.util.Scanner;
public class increasing {
public static void main(String
args[]) {
int num;
boolean flag = false;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number : ");
num = scanner.nextInt();
int currentDigit = num % 10;
num = num/10;
while(num>0){
if(currentDigit <= num % 10){
flag = true;
break;
}
currentDigit = num % 10;
num = num/10;
}
if(flag){
System.out.println("Digits are not in
increasing order.");
}else{
System.out.println("Digits are in
increasing order.");
}
} }
Exercise 8: Create a method to check if a number is a power of two or not
Method Name checkNumber

Method Description Checks if the entered number is a power of two or


not

Argument int n

Return Type boolean

Logic Check if the input is a power of two.

Ex: 8 is a power of 2

Answer:package Tushar;
public class check {
static boolean check_power(int n ) {
while(n%2==0)
{
n=n/2;
}
if(n==1)
{
return true;
}
else
{
return false;
}}
public static void main(String s[])
{
int n = 8;
System.out.println(check_power(n)); }
}

Output: “true”

You might also like