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

Logic Building Questions With Solutions

Uploaded by

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

Logic Building Questions With Solutions

Uploaded by

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

Logic Building Questions

Arrays

1. Write a program to find the second largest number in an array.

Solution: Sort the array and return the second last element.

2. Reverse an array without using an additional array.

Solution: Use a loop to swap elements from start and end until the middle.

3. Rotate an array to the left by a given number of positions.

Solution: Use a temporary array or adjust indices manually for rotation.

4. Count the occurrences of each element in an array.

Solution: Use a dictionary to count occurrences of each element.

5. Check if two arrays are permutations of each other.

Solution: Sort both arrays and compare each element for equality.

6. Merge two sorted arrays into one sorted array.

Solution: Merge the arrays while maintaining sorted order using two pointers.

7. Find the subarray with the maximum sum (Kadane?s Algorithm).


Logic Building Questions

Solution: Use Kadane's Algorithm to find the subarray with the maximum sum.

Strings

1. Check if a string is a palindrome.

Solution: Reverse the string and compare it with the original.

2. Reverse each word in a string without changing the word order.

Solution: Split the string by spaces, reverse each word, and join back.

3. Count vowels and consonants in a string.

Solution: Iterate through characters and count vowels/consonants using conditions.

4. Find the first non-repeating character in a string.

Solution: Use a dictionary to count character frequencies, then find the first unique character.

5. Remove all duplicate characters from a string.

Solution: Use a set to keep track of seen characters and build a new string.

6. Check if two strings are anagrams of each other.

Solution: Sort both strings and compare them for equality.


Logic Building Questions

7. Implement a function to compress a string (e.g., 'aaabb' -> 'a3b2').

Solution: Iterate through the string, count consecutive characters, and build the compressed string.

Functions

1. Write a function to calculate the factorial of a number.

Solution: Use a loop or recursion to multiply numbers from 1 to n.

2. Create a function to check if a number is prime.

Solution: Iterate from 2 to sqrt(n) and check divisibility; return True/False.

3. Implement a function to generate Fibonacci numbers up to `n`.

Solution: Use a loop or recursion to generate Fibonacci sequence until n.

4. Write a function to calculate the power of a number using recursion.

Solution: Write a recursive function to multiply the base number for the exponent times.

5. Implement a function to find the GCD (Greatest Common Divisor) of two numbers.

Solution: Use the Euclidean algorithm to calculate the GCD recursively.

6. Write a function to convert a decimal number to binary.


Logic Building Questions

Solution: Divide the number by 2 repeatedly and store remainders to convert to binary.

7. Implement a function to find all permutations of a given string.

Solution: Use recursion or backtracking to generate all permutations of the string.

Object-Oriented Programming (OOPs)

1. Create a class `BankAccount` with methods to deposit, withdraw, and check balance.

Solution: Define a `BankAccount` class with methods `Deposit`, `Withdraw`, and `GetBalance`.

2. Implement a `Rectangle` class with methods to calculate area and perimeter.

Solution: Define a `Rectangle` class with attributes for length and breadth, and methods for area

and perimeter.

3. Design a `Person` class with properties for name, age, and a method to display the details.

Solution: Create a `Person` class with attributes for name and age, and a method to display details.

4. Create a `Shape` base class and derive classes for `Circle`, `Square`, and `Rectangle`.

Solution: Create a base class `Shape` and derive specific shapes like `Circle` with additional

attributes and methods.

5. Demonstrate method overloading and overriding using a `Calculator` class.


Logic Building Questions

Solution: Define methods with the same name but different signatures (overloading) and override

base methods in derived classes.

6. Design an abstract class `Vehicle` with a method `StartEngine` and derive classes for `Car` and

`Bike`.

Solution: Create an abstract class `Vehicle` with an abstract method `StartEngine`, and derive

classes `Car` and `Bike` with implementations.

7. Implement a `Student` class with properties, constructors, and methods for managing student

data.

Solution: Define a `Student` class with constructors for initialization and methods to manage student

data.

C#

1. Write a program to demonstrate `foreach` with a collection.

Solution: Use a `foreach` loop to iterate through the elements of a collection like an array or list.

2. Implement a generic method to swap two variables.

Solution: Define a generic method with `ref` parameters to swap values.

3. Use LINQ to find the largest and smallest numbers in a list.


Logic Building Questions

Solution: Use LINQ queries like `.Max()` and `.Min()` to find values.

4. Write a program to handle exceptions for division by zero.

Solution: Wrap the division code in a `try-catch` block to handle exceptions.

5. Create an interface `IShape` with methods for area and perimeter, and implement it in `Circle`

and `Rectangle`.

Solution: Create an interface `IShape` and implement methods `GetArea` and `GetPerimeter` in

derived classes.

6. Write a program to demonstrate the use of `async` and `await`.

Solution: Define an asynchronous method using `async` and await a task for operations.

7. Create a collection of employees and sort them by their salaries.

Solution: Use `List<Employee>` and `OrderBy` to sort employees based on their salary.

WinForms

1. Create a login form with validation for username and password.

Solution: Design a form with text boxes for username and password, and a button to validate them.

2. Design a form to add, edit, and delete items in a list.


Logic Building Questions

Solution: Use a `ListBox` or `DataGridView` to display items, and add buttons for Add, Edit, and

Delete.

3. Create a form with a `DataGridView` to display and edit product details.

Solution: Use a `DataGridView` control and bind it to a `DataTable` for product details.

4. Develop a form to calculate the sum of two numbers using text boxes and a button.

Solution: Add two text boxes for numbers, a button to calculate, and display the result in a label.

5. Implement a form with a timer to display the current time.

Solution: Use a `Timer` control to update a label with the current time at regular intervals.

6. Create a notepad application with options to save, open, and edit text files.

Solution: Use `RichTextBox` for editing text, and provide Save/Open buttons with file dialogs.

7. Design a simple calculator application with basic arithmetic operations.

Solution: Use buttons for operations and text boxes to display input and results.

You might also like